Better emacs

I really try not to get involved in OS/distro/editor wars, but I can’t help jumping in on a little hot-advocacy-action. There is an article on 10 ways to be more effective in emacs that keeps popping up toward the top of the reddit link list that has been totally driving me nuts. So, I just can’t keep from posting a response.

I use emacs pretty much all day every day. I cut my teeth on it back in 1993 while at college, borrowing a .emacs file from an upper-classman and jotting a few important key combinations on a piece of green-bar paper. Since then, I’ve programmed in a dozen languages for a handful of companies as well as just out of pure joy. Emacs is what I choose to use (although I’m also fluent in jedit and vim). I’m also a “configuration minimalist”: I like to use programs as they come out of the box. I find it easier to adjust my brain to how a program works than forcing a program to work how I think. After trying to carry around a set of dot-files and being frustrated when a particular system lacked my particular bindings (I work with around 20 machines in a given week) it just became more time efficient for me to work within defaults with very few settings.

Responses

First, some responses to the author’s points. I won’t hit on every point, but there are a few I care about:

Item 1: Swap Caps Lock and Control - I almost agree with this. If you’re going to use emacs, you’ll live with your pinky on the Ctrl key, so it is worth it to remap it to a faster and less stressful position on the keyboard. But who actually uses Caps Lock? In the very rare instance that I need to type a few capital letters in sequence (I don’t write much Apple BASIC anymore), Shift works just fine. I say ditch the Caps Lock key altogether. Modern X servers make this easy with the XKbOptions “ctrl:nocaps” setting; one line in your X config makes this change permanent, and it’s less confusing than an xmodmap incantation (which I always have to search around the internet for). Plus, there is a very nice little utility over at Sysinternals that lets you do this on Windows (it’s called Ctrl2cap). This is an exception to my configuration minimalizism, but your hands will thank you.

Item 2: Invoke M-x without the Alt key - Seriously? I do remember back when I used a Visentech 220E that some of the older machines at school didn’t have a working termcap that let the Meta key work. But this is an artifact of a time gone by; I haven’t encountered a machine with a non-working meta key for ages. If your platform can’t map Alt to Meta, you need a better platform. Just use Alt, and you’ll be fine. The core keys you need to use are mapped to Control (which we fixed above), so grabbing the Alt key for M-x commands won’t be so bad.

Item 3: Prefer backward-kill-word over Backspace - I agree with the heading, but not the content of this item. By default, this function is mapped to M-Backspace which I find to be pretty quick, not to mention how enjoyable whacking a large Backspace key can be. You already use Backspace, cording it with the meta key should be a trivial brain-training excercise. But remapping it to C-w is right out. If you need to move code around, having quick access to C-w’s default mapping (kill-region) is critically important. Want to move a block of code? Mark the beginning with C-Space, move to the end and kill the region with C-w. Then just move the cursor to the destination and mash C-y to yank it into place from the kill ring. This key binding change is sure to drive you nuts on a new system, or cause frustration for anyone who tries to “drive” your editor for a moment or two.

Item 4: Use incremental search for navigation - Oh hell yes. Mastering the use of search to find a function, variable, or comment is incredibly useful. But don’t forget to learn how to move a line at a time (C-n, C-p), a page at a time (C-v, M-v), or to the beginning/end of a buffer (C-). I learned a few of these later in my learning curve than I should of. Also, don’t under-estimate the power of moving forward and back a character (C-f, C-b) or word (M-f, M-b) at a time without leaving the home row.

Item 5: Use temp buffers - Certainly, but you have have a scratch buffer sitting there for you automatcially: go ahead and use it. It also has the advantage of not asking if you want to save it when you exit emacs.

Item 7: Lose the UI - I’m not going to tell anyone to remove the UI. It doesn’t gain you a damn thing. If you like using the mouse, use the mouse. And in this modern GUI world I often use the mouse with other applications and have to switch to and fro. As much as I love emacs’s keyboard-centrism, I’m not going to handcuff myself from using the mouse. I work with a browser window open constantly and as a result use the mouse plenty. If I need to go back to emacs to look at something, removing the UI forces me back to the keyboard. A great amount of use efficiency comes from avoiding switching from keyboard to mouse or back, not from only ever using one.

Interestingly, the author goes on a bit of a sidebar about keyboard efficiency here and presents some examples. Sadly, his examples don’t work for me. If I want to get to the top of a buffer and add 80 hyphens, I do C-< to get there, type C-8 C-0 (to tell emacs to repeat the next thing I type 80 times) and hit “-”. I think he’s got some other custom key bindings he’s not telling us about (that’s what I call “bad advocacy”).

My Additions

There are quite a few little gems in the author’s footnotes and the comments he collected. Some are worth highlighting, and I’ve got some additions of my own.

Print a quick-ref - I you’re just getting started, or even want to brush up, it never hurts to have the quick reference card laying around. Look up a command a few times and it will quickly become a part of your arsenal. While you’re at it, a bookmark to the emacs manual never hurts.

Use fill-paragraph - This is handy, especially if you’re writing documentation in RST, HTML, a code comment or even composing an email. Sadly, the author’s binding doesn’t work. This function defaults to M-q today. Again, a binding he isn’t telling us about!

Flip buffers fast - The author mis-speaks when talking about switching buffers: to get a buffer list, use C-x C-b. C-x b just gets you access to the minibuffer where you can type the name of a buffer you want to visit. It defaults to he last buffer you visited, so hitting enter is a quick way to flip between two buffers.

Kill lines - If you aren’t already using C-k to kill to the end of a line, start now. It comes in especially handy in the mini-buffer to pick a buffer or file to go to.

Visit files - Open the wrong file? Use C-x C-v to “visit” the right file, replacing the current buffer. This will help you avoid having a too-long list of buffers open when you only need a few. And when you’re done with a file, C-x k will let you kill that buffer.

Use tab-completion in the mini-buffer - Whether you’re opening a new file or switching buffers, the mini-buffer has tab completion to help you avoid typing any more than you need to. Type part of the name, hit tab, and it’ll fill in the rest for you. If it beep at you, hit tab again to see a list of possible completions.

Try emacs-tramp - This one is a little more non-trivial. emacs-tramp is a virtual filesystem extension that lets you edit files on remote systems via the likes of ssh/scp/sftp and FTP. If you want to edit on a remote machine without suffering from terminal lag, this is a must-have. It also allows you to edit on your local machine via su or sudo, which is incredibly handy for touching up that httpd.conf.

jedit has a plugin that does this very well and is generally less of a hassle to get set up right, but don’t think emacs only has ange-ftp anymore.

Enable global-font-lock mode - Possibly the worst-named option in emacs, this is what gives you syntax highlighting. Maybe you won’t seem as hard-core with your curly braces displaying in pink, but you’ll probably catch some trivial mistakes sooner rather than later.

Learn version control commands - emacs comes with a version control mode, so you can edit and commit without leaving your little emacs world. If you’re writing code, switching over to a shell to type “cvs commit” is a waste of time. There is integration or modes for most source control packages (including the popular Subversion and Perforce), so look into it.

Geek out with M-x shell - You can get a shell prompt inside emacs and treat it just like a buffer. If you find yourself wanting to run a quick command and pull the result of it into a buffer, this is for you. Run a command, and you can trapse around the output just like any other buffer. Granted, this is the sort of thing that makes people call emacs a “great operating system”, but it’s just too handy to ignore. I’m also a command line geek, so I pull this out with some regularity for perl on-liners or MySQL queries.

Check out ispell - This really won’t make you more effective, but it will definitely annoy your pedantic cowokers less when your javadoc or pydoc only spells “ennumerate” in a single way. M-$ checks the current word, and there are also commands for checking entire buffers or selected/marked regions.

I hope you find this helpful. Give emacs a try!