11

how to change the current working directory of emacs?

set default-directory doesn't not affect the emacs process's current working directory, which can be verified via lsof -p pid.

  • 1
    Emacs, being "a kind of OS" doesn't really have a single current working directory. So what you see as the process's cwd is largely accidental and not really documented. Looking at Emacs's C code, I get the impression that there is currently no way to do what you want. You might hence M-x report-emacs-bug and request it as a new feature. – Stefan Nov 21 '17 at 18:47
  • 1
    Assuming you are on Linux, you could force the cwd to change by working at OS level, see unix.stackexchange.com/questions/281994/… for details. – TTimo Sep 25 '18 at 14:54
4

I think the only way Emacs offers to control the cwd of the process is the --chdir command line argument. Once the process is started, there is currently no way to modify it.

15

M-x cd

This should solve your problem.

  • 1
    According to the docs, cd will "Make DIR become the current buffer's default directory." I would presume that it's a per-buffer value and wouldn't apply to other existing buffers or to newly created ones. – mojo Feb 14 '19 at 14:52
  • @mojo perhaps, but if you do that with the default buffer when all your other buffers are closed that's all you're left with anyway. So it's still quite practical – Keldon Alleyne Mar 4 at 1:33
5

C-h f cd:

cd is an interactive compiled Lisp function in files.el.

(cd DIR)

Make DIR become the current buffer’s default directory.

If your environment includes a CDPATH variable, try each one of that list of directories (separated by occurrences of path-separator) when resolving a relative directory name. The path separator is colon in GNU and GNU-like systems.

And please explain why changing default-directory does not also change the "current directory" for you: (setq default-directory "/my/favorite/dir").

  • I mean the cwd of the emacs process, which is an entry in the process control block, and it can be easily checked via =lsof -p pid=. The =default-directory= is an internal thing to emacs. Why I need to change cwd is because tmux uses the cwd of a foreground process as the start directory of a new window/pane. – FunkyBaby Nov 21 '17 at 3:05
  • I see. Then your question was (and still is, to me) unclear. What does it mean to change the current working directory of the Emacs process? You want to change it after the process exists? More than once? – Drew Nov 21 '17 at 3:09
  • it is literally changing the =cwd= of emacs. "More than once"? what do you mean by that. =cwd= is not designed to be a fixed thing. In c, man 2 chdir. In python, os.chdir. I want to know how to do that in elisp (for emacs). – FunkyBaby Nov 21 '17 at 14:57
  • See @Stefan's answer. – Drew Nov 21 '17 at 19:33

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.