torsdag 3 februari 2011

Coming Home to Emacs

Some background

About six months ago I decided to switch editors from my old workhorse Komodo Edit to something a bit more powerful: Emacs. I don't remember exactly what triggered this change of heart. Maybe it was the fact that I was planning to give Clojure a shot, having installed ClojureBox a long time ago but never bothering to start it. Or maybe it was Steve Yegge's excellent blog post about why everybody should use Emacs. Because, you know, he works for Google so he should know what he's talking about, right?

The first week of using Emacs I was averaging about ten words a minute; between consulting Emacs cheat sheets and lying paralyzed on the floor from RSI-induced pain I didn't have much time left for actual typing. However, as time passed, I became more and more proficient using the keyboard. I remapped my Caps Lock key to Ctrl, installed a nice color theme and fell in love with org mode.

After about a month of Emacs usage my touchpad had started collecting dust and I was happily hammering away at my keyboard, confident that I had at last found the one true editor.

Enter Vim

In case you didn't know, there's a popular alternative to Emacs called Vim. There has been some debate online about which is the better editor, so in the interst of fairness I decided to try them both.

So, I downloaded Vim, got some plug-ins and went cold turkey. I decided I would use Vim for at least a month before I went back to Emacs. However, my stay in Vim-land became much longer than I had expected.

Emacs, while different from most GUI-based editors, doesn't look that unusual at first sight. The first stumbling block for Emacs novices usually doesn't come until they try to save their file by hitting Ctrl-S. Upon failing, there is usually some swearing, followed by even more cursing when they realize that saving is accomplished by entering the combination Ctrl-X-S. Informing them that releasing Ctrl before typing the S results in a different command usually doesn't help. And if you are really unlucky you'll tell them that Ctrl-X U undos, they try it but forget to release Ctrl, and get molested by a prompt that asks them strange questions about something called "uppercase-region". But I digress.

Vim, on the other hand, behaves very differently from GUI-based editors. Incredible as it may seem, it bests Emacs in the grief and confusion department by perplexing beginners from their very first keystroke. Typing in Vim seems to do nothing, or produces completely unexpected results. This is because Vim uses a very different conceptual model - modal editing.

Modal Editing

The basic idea behind modal editing is that entering characters is just one of many ways of manipulating text, which is especially true when coding. Following this line of reasoning, Vim is controlled primarily with the character keys on the keyboard, without the need for using modifier keys like Ctrl and Alt.

As an example, hitting the "j" key in Vim will not produce a "j" on the screen, but will instead move the cursor one line down. To insert text, one must enter Vim's insert mode, and then go back to normal mode to access features like moving around in the document, searching, replacing, etcetera.

This way of editing might seem terribly unwieldy at first, but it is incredible powerful once you get used to it. For example, if I want to swap the position of two lines I can just go "ddp" and be done with it. Or, lets say that I want to copy the text inside a pair of square brackets - "yi[" does it.

It was this incredbile editing power that got me hooked on Vim; at last, I thought, I had found the one true editor.

Coming home to Emacs

A couple of weeks ago me and some other interns at the company where I'm doing my thesis were tasked with making a game in a week. It was a fun challenge and a nice break from my thesis work. However, we had decided on targeting Android, which meant we would have to write the game in Java, something I was less enthusiastic about. I was even less enthusiastic about Eclipse's support for keyboard-only editing. Don't get me wrong, I think Eclipse is a very good IDE, but it's really not designed for use without a mouse. The closest I could get to a reasonable keyboard-only workflow was to use Eclipse's built-in Emacs key bindings. I still had to use the mouse now and then, but it was sufficient for the time being.

Having finished the project I realized the Emacs way of text editing wasn't half as bad as I had remembered it. Sure, it felt a bit uncomfortable having to hammer that Control key as if I was telegraphing my source code, but I could live with it. So I said to myself, "Hey, why not give Emacs another shot?". So I did, and it felt a bit like coming home to a warm cup of cocoa.

Emacs VS Vim

In my opinon there are several areas in which Emacs trumps Vim and vice versa. The great shame here is that many of the great features of Emacs are orthogonal to the ones in Vim, which means that it wouldn't be impossible to combine them to make one beast of an editor. There have been attempts to do this, but none of the flavours I've tried has stuck with me the way pure, undistilled Emacs or Vim did.

Emacs and usability

What really struck me when I came back to Emacs was that it felt a lot more usable than Vim. Take buffer switching, for example. In Vim, if you want to jump to a buffer but you forgot its name (or maybe you just restarted Vim and don't remember whether you opened the file or not) you have to start typing the name and see if it will tab complete. Or you can list all the open buffers with "ls", then switch buffers.

In Emacs, the buffer switching command lists all buffers, and will highlight them as you type, indicating which buffer it will switch to if you hit Enter. By default, hitting Enter without typing anything will switch back to the last visited buffer, which is often the behaviour you want.

Another example is Emacs' incremental search feature: Typing in the incremental search prompt will highlight all matches in the buffer, with the next match in a different colour. This makes it easier to see where you can jump to, and exactly how many more characters you need to type.

There are more small features like this (for example how emacs defaults to the directory of the current buffer when you open a file), that might seem insignificant by themselves, but they add up to a very pleasurable user experience.

Emacs and utility

Emacs comes with batteries included. You get a "scratch" buffer for jotting down notes you might want to throw away later. You get an email client, a file browser and if you install OrgMode you get a really cool task management program - all within Emacs. Why would you want to do all of this inside your editor? Because all of these applications are text-driven by nature, and Emacs is made for manipulating text. This is the origin of the phrase "Emacs is a great operating system – the only thing it lacks is a decent editor".

Emacs is also a fully-featured LISP environment, which is the source of much of its power and flexibility. It is fully programmable, and the tight integration with LISP makes it easy to extend or expand upon Emacs' functionality.

Emacs and flexibility

When I first tried out editing Python in Vim I was pretty disappointed---line breaks in function calls wouldn't wrap to PEP8 standards, there was no way to toggle comments, sometimes it wouldn't indent blocks properly, etcetera. Of course, there are plugins to address these issues, and others, but in Emacs it just works, right out of the box.


The problem with this whole Emacs vs Vim argument is that both editors are so good. If one of them were crap, its followers would (hopefully) realize the error of their ways. This also leads to a condition known as "editor envy" where Vim users envy Emacs users for things like OrgMode, and Emacs users envy Vim users because their hands haven't turned into claw-like appendages from overuse of modifier keys.

And so, the fervor with which these two camps defend their editors of choice does not stem from a deep inner conviction that they are right; rather, it comes from a nagging doubt that says they might be wrong.

Ironically enough, after writing this blog post and using Emacs at my internship for a week, I started to develop pains in my arms and wrist---the bane of all Emacs users. So, since my plans of switching to Emacs have been foiled it's back to Vim again!