Can someone provide a hook to change directory into the current directory of emacsclient when exiting emacsclient?

I often navigate to some files via terminal, pop open emacsclient to edit some files, then exit emacsclient to run some shell commands on files in the same directory. I find this more satisfactory than some other options that involve opening a terminal inside emacs (including popshell, M-x !, multi term, etc)

However, I also sometimes visit files in other directories while using emacsclient, then want to exit emacsclient to run some shell commands from the same directory where I was just editing files. However, when I exit emacsclient, I return to the original directory where I opened emacsclient, then have to navigate to the same directory using cd. This is inconvenient & leads to errors when I do not notice since the file structure within directories is similar/identical.

More background info: I start emacs --daemon, then open emacsclient -nw in a terminal when I have some quick files to edit in or near my current working directory. I've found that 'C-x C-c' will exit emacsclient but also kill the emacs daemon, so I use 'C-x 5 0' or 'C-x #' to exit just the client.


emacsclient is an ordinary shell command, so it cannot change the shell's idea of what the current directory is.

You can, however, call emacsclient like this:

$ cd "$(emacsclient -e '(progn (find-file ....) (print default-directory))')"

i.e., have emacs print the new directory name when the client returns.

It will not be easy to get right, and the effort is not worth it.

If you are doing more than a simple edit in your emacsclient, you should be using emacs and its shell.

  • You are right that I was somehow thinking emacsclient was running inside emacs server and hoping for an elisp solution. I'd possibly like to expand on your suggestion, but I'm not sure how. It sounds like you know that code example doesn't work. – raco Apr 3 '14 at 19:32
  • Sometimes it is easier to learn to like an existing tool than to teach it to behave the way you like. – sds Apr 3 '14 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.