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 directly 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 pressedpress C-x C-f Emacs should recognize that I have sshed to the rightsome machine, and use tramp to run find-file on the corresponding machine and remote path.
This takes me to the following two questions:
- How and why does the snippet above work?
- How can I extend it so that it can also track my remote paths after I ssh into another machine?
 
                