Quickly Learn emacs and vim
Programming as part of a large project, such as building a new C++ compiler from scratch, is vastly different compared to a smaller task, like writing a script to answer a random Project Euler question. Large projects typically involve too many files interacting with each other to work on a single program in isolation (for instance, it can be confusing to know what methods to refer to from another program in a file), so using an advanced integrated development environment (IDE) like Eclipse is probably the norm. For smaller tasks, two of the most commonly used text editors by programmers are emacs and vim.
One of the biggest problems that people new to these editors face is having to memorize a bunch of obscure commands. Emacs commands often involve holding the control or escape/meta key while pressing some other letter on the keyboard. With vim, users have to press escape repeatedly to go into “insert” (i.e., one can type things in) versus “normal” (i.e., one can move the cursor around, perform fancy deletions, etc.) mode.
Once one becomes used to the commands, though, emacs and vim allow very fast typesetting. If you watch an emacs or vim master type in their preferred text editor, you will be amazed at how quickly he or she can perform fancy alignment of text, advanced finding/replacing involving regular expressions, and other tasks that would otherwise have taken excruciatingly long using “traditional methods.” (Unfortunately, these people are hard to find….)
So what’s the quickest way to get started with these editors to the point where someone can write a small program? In my opinion, the best way is to go through their tutorials. Open up your command line interface (e.g., the Terminal on Macs) or your method of opening these editors.
- For emacs, type in “emacs” and then perform control-h (hold the control key while pressing “h”) and press “t.” In emacs terminology, C-h t.
- For vim, type in “vimtutor” from the command line.
This method of learning is excellent since you get a copy of the tutorial and can go through steps and exercises to test out the commands while navigating or modifying the file. I think the vim tutorial is better because it’s more structured but again, both of them do the job. They emphasize, for instance, that once a programmer gets used to the commands to move the cursors, using them will be faster than resorting to the arrow keys since one doesn’t have to move hands off of the keyboard:
- From emacs: There are several ways you can do this. You can use the arrow keys, but it’s more efficient to keep your hands in the standard position and use the commands C-p, C-b, C-f, and C-n.
- From vim: The cursor keys should also work. But using hjkl you will be able to move around much faster, once you get used to it. Really!
I have to confess that I didn’t learn emacs by using their tutorial. I just went by a list of common commands given to me by my professor and picked things up by experience and intense Googling. As a result, I experienced a lot of pain early on in my computer science career that could have been avoided by just reading the tutorial. (The control-g command to quit something was a HUGE help to me once I read about it in the emacs tutorial.) And judging by the emacs questions that other Williams College computer science students have asked me, I can tell that not everyone reads the tutorial.
So read the tutorials before seriously using these text editors.
Of course, one should decide at an early step of his or her programming career: emacs or vim? This is certainly a non-trivial task. They both do basically the same thing, and with the variety of extensions and customizations possible, I’m not sure there’s anything they can’t do that any other text editor can do, to be honest. If I had to give a general rule, I’d just go with choosing the one that felt best to you in the tutorials, or the one that’s most common among your colleagues, professors, or other peers.
And besides, it’s difficult to say which one of these editors is better than the other. It really depends on who you ask. I don’t have a good answer to this so I’ll opt-out and provide a lame one: the one that’s better is the one that you can use best.
Personally, I started my programming career using the emacs text editor because that was the preference of my Computer Organization professor, so I didn’t see the need to deviate. Within the past year, I substantially increased my emacs usage to the point where I was using it almost everyday for most typing tasks, including LaTeX, and my pinky finger didn’t like the constant reliance of the control key. So I’m giving vim a shot.
It’s still useful to know the emacs commands, because they often work in other places. For instance, when I type in blog entries here, I can use a subset of emacs commands while typing in my entry in the built-in editor for WordPress, which was surprising to me when I first found out. Instead of deleting a line by using the mouse or Mac trackpad, I can use the ctrl-k emacs command to do that. And many IDEs will have support for emacs or vim commands, so one can get the best of both worlds. I know that Eclipse has this, but I was personally disappointed that certain emacs commands didn’t work.
To wrap up this post, I suggest that knowing one of emacs or vim well is important for (computer science) graduate school since programming is a huge part of research today, so the faster things get done, the better. Moreover, university professors tend to take on more of a “leader” role rather than someone who “gets things done” (I’m not trying to disparage professors here). In other words, they tell the graduate students what to do in research projects and the students will write the necessary programs.