njs blog

Stochastic descent

Emacs has a lot of stuff in it. Like, really, a lot. Most of the time it doesn't matter, you can just use whatever bits you know and be happy, but there's always more to learn.

For example, here's a thing I often want to do: starting from whatever file I have open, open the directory containing that file.

So my fingers have memorized the sequence: C-x C-f C-j

(For the uninitiated, that's emacs-ese for "while holding down the control key, press X F J in that order".)

This breaks down as:

  • C-x C-f: this runs the find-file command (or actually ido-find-file in my case), and causes emacs to ask for the name of a file to open.

  • C-j: The prompt opens with the current directory filled in as the initial value, so this says "yes, I want that". It's like hitting enter, except that if you just hit enter without having typed anything then emacs figures you made a mistake and want to cancel out; C-j skips that logic and accepts the default.

    (Allegedly, this is intuitive: C-j often does something similar to hitting enter in emacs, and in other classic terminal tools. This is because in ASCII, the newline character has character code 10, J is the tenth letter of the alphabet, and on old-school terminals the "control" key was a modifier where hitting the Nth letter of the alphabet would send the raw ascii control code N. You see: intuitive. Looking at the ASCII table, then this also explains why your terminal sometimes freaks out when trying to distinguish between C-h and backspace [1], why C-d in unix command-line tools means end-of-file, why C-g is traditionally used for interruptions, and why Windows files on Unix sometimes end up displayed with ^M crud. The more you know 🌠)

Anyway, I've been typing C-x C-f C-j multiple times a day for, uh. 15 years now? Probably more?

Last night I missed a keystroke, and accidentally typed: C-x C-j

And it worked!

It turns out that C-x C-j is dired-jump, which opens a dired buffer for the directory containing the current file, and then puts the cursor on top of that file. So actually it works even better than C-x C-f C-j, which doesn't do that last part. (Also, if you're already in a dired-buffer, C-x C-j takes you up to the parent directory.)

I never knew this existed, even though it's been lurking there forever, just 1 Levenshtein distance away. As far as I know, the similarity is a complete and utter coincidence – one "j" is short for "jump", and the other is short for "the tenth letter of the alphabet".

I've probably used it 20 times since last night.

[Update, 2016-10-29: I'm informed that the C-x C-j key binding isn't present by default, but requires that you have loaded the dired-x package. You can do this by adding some autoload nonsense to your .emacs, or just (require 'dired-x). While you're at it you should turn on dired-omit-mode, which is apparently why I had dired-x in the first place.]

(While we're here, a random bonus emacs tip that also took me wayyyy too long to discover: in dired-mode, try hitting C-x C-q. It makes the buffer editable, so you can go around and change the text so that it looks like it's describing the directory that you wish you had – you can rename files, use search-replace, whatever – and when you hit C-x C-s, emacs will go and rearrange the real files on disk to match your changes. Plus, if any of those files are open in emacs, then the buffers will be automagically redirected to point to the new name, so you avoid the annoying thing where you rename the file in the terminal and then the next time you save emacs puts it back where it was before.)

[1]Do please appreciate the Wikipedian who labored over this page's illustrative figure + caption.

Next: Emerging from the underworld