Emacs Powertools and Opinions

I recently came across Steve Yegge’s emacs tips. He’s a man after my own heart:

“Using the mouse is almost always the worst possible violation of economy of motion, because you have to pick your hand up and fumble around with it. The mouse is a clumsy instrument, and Emacs gurus consider it a cache miss when they have to resort to using it.”

Cache miss! I love it.

His article is long, detailed, opinionated, and a goldmine of useful emacs nuggets. It will only be interesting to you if you are at least an intermediate emacs user and, say, you agree with the philosophy noted above. Here I include some of my own tips and commentary on his post.

Steve suggests mapping the CapsLock key to be Control, which is sensible in an emacs world, since you use Control orders of magnitude more often than Caps Lock, and ergonomically CapsLock has the more desirable position. But I’ve always been reluctant to do this, because it’s non-standard, and once you’ve adapted to the unusual layout, you render yourself frustrated and mistake-ridden when using anyone else’s keyboard.

Navigation: I already use Ctrl-s (search forward) and Ctrl-r (search backward) liberally to move around in documents (I agree that this is very handy!) and I do use temp buffers for notes — or rather, only one temp buffer. I only use *scratch* for this, because I like being forcibly reminded that there is no auto-save going on and anything I put there won’t be saved unless I explicitly do so (otherwise my compulsively periodic saving goes on as a background processes, in addition to emacs’s autosave, and I generally assume that anything in an emacs buffer has a good dose of permanence). However, I’ve rarely used the regexp search commands, because it was so tedious to type Meta-x-isearch-forward-regexp. Happily, in this article I learned that there are bindings to be had: Ctrl-Alt-s and Ctrl-Alt-r, respectively. (Actually, on my Mac they are Ctrl-Apple-s and Ctrl-Apple-r.) I agree that this is a spectacularly awkward binding to type, but I don’t use it that often (yet) so I’m willing to try it out before remapping it.

The other super-useful navigation bindings that I regularly use (not mentioned in his article) are Ctrl-[ and Ctrl-] to jump to the top and bottom of the file, respectively.

Buffers and windows: I already use most of the buffer and window management commands he listed (splitting, swapping, closing, and listing buffers) but did learn a new one: Ctrl-x + to balance window sizes. Cool!

GUI stuff: I don’t use the menu bar in emacs (sometimes it’s there, sometimes not; I generally ignore it either way). I do, however, like having a scroll bar. I don’t use it much (again, I dislike using the mouse), but it gives a nice visual indication of where I am in a file and how big it is. Yes, the status bar will tell me that I’m 61% through the file, but the visual is much more intuitive, especially because it shows me how much of the file, proportionally, is currently shown in the buffer window. Steve backs off from his initial recommendation to get rid of the scroll bar with an interesting analog/digital argument.

Steve cites “region selection” as a case where the mouse is actually helpful in emacs. I’ll agree, in the case of rectangular region selection (I don’t even know how to do that with the keyboard, much less efficiently), but I do normal region selection all the time with the keyboard. I set the mark (Ctrl-space), move to the end, and then do whatever I needed to do with the region: copy (Meta-w) or cut (Ctrl-w), usually. He seems to view the “move to the end” as the slow bit, but with Ctrl-s (search forward), as noted above, generally this is speedy as well.

Help: I agree that Meta-x-describe-bindings can be a fascinating (and revealing) exploration of the current mode and the powers available to you. I’ve used this in bibtex mode to good effect.

Query-replace: This is one of my favorite bits of emacs power. I love that I can so easily specify text to find and replace, and do it interactively, skipping from instance to instance and individually saying yes or no, or opting to just say yes to all such occurrences (with a !).

Other tidbits I gleaned from this article are

  • Meta-b to skip back a whole word. I can’t believe I never knew this one. I imagine then that Meta-f goes forward a whole word. Hey, it does! I also use Ctrl-t regularly to swap two characters (usually when I typed them in the wrong order), and sure enough… Meta-t swaps two words. Whee!
  • M-x list-matching-lines: this one blew my mind. It takes in a regexp and shows you every line in the buffer that matches it (like grep -n -e on the command line, but hey, you’re still in emacs, and if you go to one of those entries and hit return, your cursor jumps to that position in the buffer).

Any additional tips are welcome!

4 Comments
3 of 3 people learned something from this entry.

  1. Jim in PA said,

    November 9, 2008 at 7:02 am

    (Learned something new!)

    For the past several years I have been running emacs on either a Mac or Windows (cygwin) box and constantly found it frustrating to have to context switch between the standard key combos for native apps in those OS’s and the emacs system of incantations and rain dances. For about two years now, I have been using “cua-mode” (now built in to the distribution I believe) that gives me C-x, C-c, C-v, C-z for cut, copy, paste, and undo. I usually don’t mention this because the emacs faithful consider this a sacrilege worthy of excommunication from the Church of Emacs, but I found, for me, it avoids an additional class of cache miss.

  2. wkiri said,

    November 9, 2008 at 10:43 am

    (Learned something new!)

    Interesting tip — I hadn’t heard of that mode. In my opinion, emacs is about customization; the whole reason you can bind keys arbitrarily is to make it accommodate the way you work, not the way someone else thinks you should work. :)

    One thing I love about Mac OS X is that my emacs navigational keybindings work everywhere, not just in emacs! I can move around in any text area, in any app, without having to use the mouse or learn new keybindings. Any text area, that is, except the Microsoft apps, which disable them. :(

  3. Charles said,

    November 9, 2008 at 7:48 pm

    (Learned something new!)

    Try ctrl-meta-a and ctrl-meta-e to jump to the beginning and end of a function, respectively.

  4. michael said,

    November 20, 2008 at 5:42 pm

    You do use aquamacs on mac, right? It enables the expected clover-foo (apple-foo) shortcuts.

Post a Comment

I knew this already. I learned something new!