I use the shell command via C-u M-x shell to open a shell on a remote server by giving as default directory the path to the remote server (e.g /[email protected]:/home/user) (which should have the same effect as the method described in this answer).

The directory of the buffer is at the beginning in sync with the directory of the shell (/[email protected]:/home/user`), and tab completion works as expected giving me choices from the remote directory.

However, when you issue

cd ~

in the remote shell, the buffer directory is set to the local home path, so that I get tab completion for local directories which obviously does not make any sense in the remote shell.

How can I keep the directory tracking in sync even when I use cd in the remote shell? - I'm using GNU Emacs 23.1.1 on Centos, remote machine is Linux too.

  • Perhaps C-u M-x shell and C-x d /ssh:username@hostname:/path return M-x shell aren't equivalent? Does shell completion still work if you use the longer workaround?
    – Melioratus
    Nov 13, 2016 at 17:42
  • Will check on Thursday when I'm back at that machine.
    – halloleo
    Nov 14, 2016 at 7:58
  • 2
    I have the same problem with C-x d /ssh:username@hostname:/path return M-x shell . In fact, I found out how to force the out-of-sync problem: just do cd ~ in the remote shell! (Will update the question accordingly.)
    – halloleo
    Nov 17, 2016 at 5:36
  • Thanks for update! It sounds like you found the answer so you should post it! Are there any other commands like cd ~ that will force shell to sync without doing a change directory?
    – Melioratus
    Nov 17, 2016 at 15:49
  • Nooooo! I haven't found a solution - I just found out a straight forward way to reproduce the issue - see the updated question.
    – halloleo
    Nov 18, 2016 at 7:23

1 Answer 1


Not the best solution, but certainly a working one: Use [dirtrack-mode]. The following preparations are needed:

1. On the remote machine:

Make sure the prompt of the remote shell contains the path including the server login in TRAMP notation (e.g. /[email protected]:/home/user). You can achieve this by setting PS1 (e.g. in the .bashrc file on the remote server):

export PS1="[/\u@\h:\$PWD]$ "

2. In your local Emacs:

Run the following list expression (e.g. in the .emacs file):

(setq dirtrack-list (quote ("^\\[\\(.*\\)\\]" 1)))

Then, after establishing a remote shell buffer, issue M-x dirtrack-mode in that buffer and - voila! - the buffer directory should follow the shell directory properly.

This solution isn't perfect, because it is necessary to print out in the remote shell the whole absolute path extended by the server login credentials. (Make sure the home directory gets expanded to the full path!) However it does work reliably.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.