tbc0
tbc0

Reputation: 1577

How do cd, cd-absolute, and default-directory compare in GNU emacs?

How should I think about elisp functions cd and cd-absolute and the elisp variable default-directory when I'm customizing Emacs? My experience leads me to believe that cd-absolute changes the directory for the emacs process itself (a global setting across buffers where default-directory isn't set locally), while cd and default-directory are local to the buffer. The built-in help isn't sufficient to make me comfortable in my understanding, though, and I am seeing behavior that leads me to suspect cd-absolute is overriding default-directory in buffers I'm visiting.

End-edit

If I had received no answer here, I would have had to do my own research on the help-gnu-emacs list and, only as a last resort, I would have had to read the source code.

Upvotes: 3

Views: 1370

Answers (1)

Tyler
Tyler

Reputation: 10032

I think you are misunderstanding how default-directory works. Setting default-directory in your .emacs will have no effect on most buffers. The local value of default-directory for a buffer that is visiting a file is automatically set to the directory where the visited file is stored. This will over-ride any previously set value of default-directory.

If you change the current directory of a buffer via cd (or cd-absolute), this will set default-directory only for that buffer. If you then open a new file, default-directory for the new buffer gets set to the directory of the new file.

If you open a new buffer that is not visiting file, then the default-directory value of this new buffer will be the same as the value for the previous buffer. This is the only case where the result of cd will apply to a buffer other than the one it is called from.

cd-absolute is not intended for interactive use. As far as I can tell, it's a convenience function that differs from cd only in that the path is treated as an absolute, rather than relative, path.

Given all this, what are you trying to do?

Upvotes: 2

Related Questions