4

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 /user@myserver.com:/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 (/user@myserver.com:/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.

halloleo
  • 1,215
  • 9
  • 23
  • 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 '16 at 17:42
  • Will check on Thursday when I'm back at that machine. – halloleo Nov 14 '16 at 07: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 '16 at 05: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 '16 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 '16 at 07:23
  • Sorry for the confusion! I remember having this same issue years ago and vaguely recall reading something about calling the `pwd` command in shell to trigger `tramp` to refresh the remote execution path in the buffer. I've been looking for the original source but haven't found it. Does using `pwd` help at all? – Melioratus Nov 20 '16 at 02:30
  • No, it's not that easy, because`pwd` does not show the remote host - I think the emacs command `shell-resync-dirs` has to be enhanced somehow to find out whether the shell is on a remote host... – halloleo Nov 21 '16 at 05:44

1 Answers1

3

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. /user@myserver.com:/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.

halloleo
  • 1,215
  • 9
  • 23
  • Thanks for posting! Here's another good resource related to your answer: [http://emacs.stackexchange.com/a/5592/388](http://emacs.stackexchange.com/a/5592/388). – Melioratus Nov 22 '16 at 16:06