12

Are there any options or packages which cause default-directory to be updated if the working directory changes in shell-mode and term-mode? I.e. when using cd, but the solution would have to be more robust than just look for this particular command and maybe a few others - it should actually check the value of $PWD after every command, I would think.

More importantly though it should also work when connecting to a remote machine from inside a shell-mode or term-mode buffer which was first created with default-directory being a local directory. That's actually the feature I really need: being able to tell programmatically whether the commands I type into such terminals are run on a remote machine. I would like to get that information in form of a properly set default-directory and figure if there is a way to get that, then the tool doing this would also work locally as described above.

1
  • Have you looked into dirtrack-mode? It has a hook that may do what you want. Commented Dec 20, 2014 at 16:41

1 Answer 1

12

I think the first part of your question can be done with dirtrack-mode. First, you set your shell prompt to include the present working directory. Next, you set dirtrack-list to a regex that tells dirtrack-mode how to extract it. Example: I set PS1 in Bash to be this:

export PS1="[\h:\w] $ "

and in Emacs I set dirtrack-list and turn on dirtrack-mode:

(set-variable 'dirtrack-list '("^.*[^ ]+:\\(.*\\)]" 1 nil))
(dirtrack-mode 1)

(Taken from this great blog post.)

After that, I'm able to change directories at will, and default-directory changes as I go. This also works if, in a shell, I SSH to a remote machine where I've set the prompt the same way -- cd /etc/apache2, on the remote machine, changes default-directory to /etc/apache2. Note, however, that this is local -- in the case of the remote machine, default-directory will still be set to /etc/apache2 on the local machine.

If I understand the second part of your question, you want default-directory to be set to something like remote-machine:/etc/apache2 when SSHing to another machine. When using ssh-el, you can run ssh-directory-tracking-mode to make this work. For example, if I run M-x ssh, cd to /etc/apache2, then run C-x C-f, the prompt I see is:

Find Files or Url: /scp:aardvark@remote-machine:/etc/apache2

You can run 'M-x ssh-directory-tracking-mode` by hand, but it's more convenient to set it as a hook for ssh-mode:

(setq ssh-mode-hook 'ssh-directory-tracking-mode)

More information:

2
  • 2
    Excellent answer, thanks! I am actually interested in this because a package I have written has grown features which will make this desirable. It's with-editor which will be used by the next release of Magit. Magit itself does not use with-editor's shell-mode/term-mode-support, so I probably won't integrate this before the release. But the information you provided will make it much easier to do so, once I am done with the Magit release and can again focus on other things.
    – tarsius
    Commented Dec 20, 2014 at 17:40
  • Here's how to get started with with-editor outside of Magit: emacs.stackexchange.com/questions/27/…. And the Magit issue which lead me to ask this question: github.com/magit/magit/issues/1638.
    – tarsius
    Commented Dec 20, 2014 at 17:41

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.