Emacs annoyance #448,546

From the Emacs docs:

Each buffer has a default directory which is normally the same as the directory of the file visited in that buffer. When you enter a file name without a directory, the default directory is used. If you specify a directory in a relative fashion, with a name that does not start with a slash, it is interpreted with respect to the default directory. The default directory is kept in the variable default-directory, which has a separate value in every buffer.

The command M-x pwd displays the current buffer's default directory, and the command M-x cd sets it (to a value read using the minibuffer). A buffer's default directory changes only when the cd command is used. A file-visiting buffer's default directory is initialized to the directory of the file it visits. If you create a buffer with C-x b, its default directory is copied from that of the buffer that was current at the time.

This is extremely annoying. Vim leaves my working directory the hell alone, why doesn't Emacs? Vim lets you set a global working directory, and selectively (and explicitly) change it on a per-buffer basis. This is what I want.

But in Emacs, every time you open a file, the working directory automatically changes to the directory of that file. If you have multiple files open in Emacs (which of course you do), every time you move the cursor between windows, or look at a new file in your current window, your working directory just changed out from under you.

So say you open some files. Then you want to start SLIME. So you C-x 2 to split and open a new window, in which to start SLIME. Except (annoyance #448,547) Emacs doesn't open a window with a new BLANK file, as Vim sanely does via CTRL-W n; instead it puts the file you're looking at into BOTH windows, which is absolutely never what you want. As a result, the working directory of BOTH windows is the directory of whatever file you were looking at. So every time you start SLIME, you have to make sure you M-x cd back to the proper working directory first, because otherwise your Lisp process is randomly going to start in the wrong directory.

But you can change the directory of the SLIME buffer after you start SLIME. Just M-x cd or ,cd. Except if you're unlucky enough to be using SLIME for Clojure, which may have set its classpath based on its working directory when you started it. In that case you have to restart SLIME.

But if you kill SLIME, Emacs (annoyance #448,548) jams another random file into the window where SLIME just was. So your working directory just changed again! Or did it? Depends what Emacs decided to put into your window. You may think that, while SLIME is running, M-x cd and/or ,cd and then ,restart-inferior-lisp may do what you want, but you would be wrong; it always reverts back to the original working directory from when SLIME was first started.

Lost? Confused? tl;dr? Welcome to Emacs. So now I'm looking through the encyclopedic tangled mess of Emacs documentation to try to figure out how to get Emacs not to change my working directory, ever, unless I say so. This is hindered as always by (annoyance #448,549) Emacs' arcane and non-standard terminology. So far, I have committed to memory that "current directory"/"working directory" in Emacs is instead called "default directory". And you don't open files, you "visit" them. Command line? No, "minibuffer".

4 Comments

http://gravatar.com/avatar/5684d042aae0fa0000785d745e6542cd.jpg?d=identicon
3752 says:

"...try to figure out how to get Emacs not to change my working directory"

Simpler solution is to switch to buffer which did not change it's working directory (scratch for example) and then do whatever is dependent on the directory value.

Feb 03, 2009 12:33 PM PST
http://gravatar.com/avatar/4d84ec3981443dfd9c287e845b60d2ce.jpg?d=identicon
Brian says:

Actually I use this now:

;; Prevent Emacs from stupidly auto-changing my working directory
(defun find-file-save-default-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-default-directory)
Feb 03, 2009 12:49 PM PST
http://gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg?d=identicon
James says:

Instead of beating your head on Emacs trying to get it to be another editor, why not learn to live with it the way it is. It's not inferior that if you try to do something from a file, it'll be relative to the location of that file. Most often it's exactly what I want.

For people used to Emacs, that's as natural as it could be. An Emacs user might as well berate vi for being modal. Work with the tool, not against it.

Try using bookmarks (C-h R, then search for bookmarks) - it's very easy, and you can bookmark directories or files that you normally start from. With quick keystrokes I go to key directories and do something from there.

I agree that split window shouldn't give you two views on the same buffer (though sometimes I do want that).

As for terminology - well, Emacs has been around a long time. Sure a window is a frame, buffers... minibuffer..., but they're all logical and many of them are terms which came into being before the currently accepted term. Again, stop beating your head against it and go with the flow. Go into the Mac world and you find suddenly some of the terminology (and cursor movement keys) are Emacs-esque.

May 01, 2009 07:25 AM PDT
http://gravatar.com/avatar/4d84ec3981443dfd9c287e845b60d2ce.jpg?d=identicon
Brian says:

@James: I have adapted where it makes sense to, but Emacs' directory-changing behavior is just broken in my opinion. Many other apps in the world have the same behavior of "remembering" the last directory you opened a file from, and it's always broken.

Especially for source code where you probably want to sit in some parent directory and do things. e.g. the directory with your makefile, or the directory where Java expects you to be when you compile your files, or the toplevel where your version control system is rooted. There are many such tools that expect you to be rooted in some directory and run commands from there. Emacs messes this up.

It wasn't that big a deal to fix in any case. Someone on Stack Overflow showed me how.

May 03, 2009 04:21 PM PDT

Speak Your Mind

This says COWS.

Preview

Commenting Help

Email / Avatar

  • Supply your email address and your Gravatar will be used.
  • I will never email you and your address won't be published.

No HTML allowed!

All HTML is auto-escaped. Use Markdown. Examples:

  • *emphasis* = emphasis
  • **strong** = strong
  • [link](http://foo.bar) = <a href="http://foo.bar">link</a>
  • `code in backticks` = code in backticks
  •     code indented 4 spaces =
    code indented four spaces
  • > Angle-brace quoted text =
    Angle-brace quoted text