tags:

views:

538

answers:

4

As per the Emacs docs, every time you open a file, Emacs changes default-directory to the directory containing that file.

Then, if the cursor is in that buffer and you (for example) start SLIME, it uses default-directory as the current working directory for SLIME. If you try to open a new file, it opens the file with default-directory as your starting point.

I want to be able to M-x cd or otherwise cd to a directory, and then never have Emacs change my current working directory to anything but that directory until I tell it otherwise. I want this to be global across all buffers, so that any time I'm doing something involving the current working directory, I know what it's set to regardless of where my cursor is at the moment. Is there a way to do this?

+3  A: 

You could try using something like this:

(add-hook 'find-file-hook
          (lambda ()
            (setq default-directory command-line-default-directory)))
link0ff
+1  A: 

This is not an answer to your question, but it might be a solution to your problem.

Take a look at project-root.

I use that to provide a Find File relative to the project root. I'll see if I can find the elisp snippet on Monday when I'm back at the office.

You could even possibly use it with the other trick mentioned in this thread to find a suitable target directory to cd into.

jplindstrom
+2  A: 

Another variant is to bind default-directory to the necessary directory in directory-local variables, e.g. in the .dir-locals.el file in one of your parent directories to something like:

((nil . ((default-directory . "~/.emacs.d/"))))
link0ff
A: 

This is the best I've come up with so far, sadly:

(defun find-file-save-directory ()
    (interactive)
    (setq saved-default-directory default-directory)
    (ido-find-file)
    (setq default-directory saved-default-directory))
(global-set-key "\C-x\C-f" 'find-file-save-directory)

This works as long as default-directory is properly set before I C-x C-f. I'm going to Accept jurta's answer for pointing me in a useful direction.

Brian Carper