This is a read-only archive!

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".

October 12, 2008 @ 8:04 AM PDT
Cateogory: Programming
Tags: Emacs, Vim, Rant


Quoth 3752 on February 03, 2009 @ 4:33 AM PST

"...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.

Quoth Brian on February 03, 2009 @ 4:49 AM PST

Actually I use this now:

;; Prevent Emacs from stupidly auto-changing my working directory
(defun find-file-save-default-directory ()
    (setq saved-default-directory default-directory)
    (setq default-directory saved-default-directory))
(global-set-key "\C-x\C-f" 'find-file-save-default-directory)
Quoth James on May 01, 2009 @ 12:25 AM PDT

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.

Quoth Brian on May 03, 2009 @ 9:21 AM PDT

@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.

Quoth Alpheus on March 17, 2010 @ 5:08 AM PDT

I'm currently trying to learn Emacs, but this is something that makes sense to me. My default editor is Kate, and I'm using Emacs because I'm currently learning Lisp, and I'm lured be the power that Lisp + Emacs offers me. This is the default behavior of Kate.

Of course, since Kate is a graphical-based editor, I have to do a lot of things with the mouse, and there are a lot of behaviors (such as a vertically-split buffer of fixed size on the left that displays all the buffers) that I'm used to in Kate, that I haven't figured out how to duplicate in Emacs yet.

I also use Vim a lot, but I've never used its full power; I've never been thrilled by "modes", and I especially despise it when I find a system that doesn't let me use the arrow keys in edit mode!

Mike H
Quoth Mike H on August 19, 2010 @ 7:35 AM PDT

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.

Actually, I use this all the time. I'm often editing a large text document or a (probably too) large code file, and want to check something at another part of the file without losing the cursor position. C-x 2 and search in the other window is often easier than only using bookmarks or tags in one window, because you can see the context and edit at both locations.

I have adapted where it makes sense to, but Emacs' directory-changing behavior is just broken in my opinion.

Graphical apps that don't remember and default to the location of the last opened file when opening the next one drive me absolutely bonkers. I suppose my home directory is too deeply nested, but clicking through the same path gets old really fast. Emacs at least has tab completion if the location is far away from the where you want to go. Anyway, I completely agree with James and Alpheus that the current Emacs behaviour is useful and it's what most people would expect these days.

Quoth Brian on August 19, 2010 @ 8:00 AM PDT

For certain apps, yes, opening a new file should default to the directory of the last file opened. I like this behavior in Gimp for example.

But for programming, I need to sit at the root of my project. It's where I git from, it's where I compile from, it's where I SLIME from, it should be the cwd when my program is running. It's one less thing to think about when I'm opening a file.

I very quickly develop muscle memory for opening files. The alternative requires me to stop and think every time: Do I have to go up 2 levels, then down 5 levels to find my file? Or up 1 level? Or 3? Or down only 4? Or 6?