Skip to main content
2 of 3
added 4 characters in body
Amelio Vazquez-Reina
  • 43k
  • 81
  • 211
  • 298

Tracking my shell path in Emacs

Not a long time ago, I found the following precious snippet that allows Emacs to track my current directory on any ansi-term buffer.

More specifically, if I cd <some/path> from within a terminal inside Emacs, and I then press C-x C-f, Emacs will run find-file from <some/path> directly, which is very, very handy.

if [ -n "$INSIDE_EMACS" ]; then
  chpwd() { print -P "\033AnSiTc %d" }
  print -P "\033AnSiTu %n"
  print -P "\033AnSiTc %d"
fi

However, the above trick doesn't work if I ssh to a remote machine from the shell. Ideally in this case, if I press C-x C-f Emacs should recognize that I have sshed to some machine, and use tramp to run find-file on the corresponding machine and remote path.

This takes me to the following two questions:

  1. How and why does the snippet above work?
  2. How can I extend it so that it can also track my remote paths after I ssh into another machine?
Amelio Vazquez-Reina
  • 43k
  • 81
  • 211
  • 298