6

When using eshell or ansi-term and bash emacs changes the default-directory variable depending on what directory you are in.

So if I move to /home/user/code/project and then use ido-find-file to open a file it starts ido with the CWD. If I use ksh (my normal shell) or zsh (tried for testing) it doesnt work. Is there a setting or is this just supported under bash?

Thanks

Adam
  • 61
  • 2

2 Answers2

10

Put this in your .zshrc:

chpwd() { print -P "\033AnSiTc %d" }

print -P "\033AnSiTu %n"
print -P "\033AnSiTc %d"

The chpwd() function is run every time the pwd changes. The line ending in %d is the one that allows you to track the current directory--I repeat it outside the function to make sure it is run when zsh first initializes. But Emacs requires us to first tell it who the current user is (the line ending in %n).

This is actually explained in term.el, which should be inside /usr/share/emacs//lisp, if you're using Ubuntu.

Hope this helps.

ior3k
  • 430
  • 5
  • 9
  • 3
    This worked great for me with one change: Test whether you are inside emacs first. `if [ -n "$INSIDE_EMACS" ]; then chpwd() { print -P "\033AnSiTc %d" } print -P "\033AnSiTu %n" print -P "\033AnSiTc %d" fi` – bengineerd Apr 10 '12 at 19:44
  • Ah, good call. It works for me like this outside Emacs too, but it does print those sequences whenever you open a terminal outside Emacs, which can be annoying. – ior3k Apr 13 '12 at 08:35
0

There are a number of solutions to this problem. I happen to prefer my implementation which changes the prompt to contain the current path, which Emacs recognizes and uses to get the behavior you want. Additionally, my solution hides the portion of the prompt which contains the path because I find it ugly.

My solution can be found in the blog post: Emacs Tip #25 Shell Dirtrack By Prompt.

However, there are a number of other solutions which can be found on the Emacs Wiki or other places:

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 2
    I'm not sure that this solution is applicable to that particular problem - shell-mode and term-mode are completely different beasts. – Bozhidar Batsov Aug 18 '10 at 05:40