Emacs or Vim as a programming editor (part 2)

Grab the feed

School is a twelve-year jail sentence where bad habits are the only curriculum truly learned.

John Taylor Gatto

Recent comments

This is the second of a three parts series about Emacs and Vim as programming editors. I've briefly reviewed Emacs in the first part and now will talk about Vim.

Vim

Vim is short for vi improved and was written by Bram Moolenaar based on the original vi created by Bill Joy, another legend of the hacking community, in 1976. A good year for text editors it seems, uh?

In an interview Bill revealed that while he was coding vi he didn't have access to the fast connections and powerful machines the MIT dudes had. This may be the cause for vi to require modest resources and less complicated keystrokes than others editors.

Vim has two modes of operation, the insert mode, in which you actually enter text into your documents, and the command mode, to modify the text, manage files, run searches and complete other jobs. Some think the use of two modes is confusing but I don't see why the big deal, you can enter command mode just by pressing Esc and go back to insert mode with keys such as i for inserting or a for appending, I find those pretty obvious.

It's also good to remember that you don't need a mouse at all when using Vim or Emacs, most modern editors depend on a mouse and their users waste a lot of precious time moving the hands from keyboard to mouse and back.

Vim's choice of directional keys is pure genius: h, j, k and l. It may make no sense at first but once you put your right hand over the keyboard you'll notice what I mean, the keys are exactly at your fingertips.

Vim uses a few key chords, like Shift+V for entering one of the visual modes (ok, there are more than the two modes I mentioned above but once you're in love with Vim it won't matter), but the most common tasks require just one key press. The dot key is an impressive time saver to repeat operations and combined with a search and next search (n) can do wonders on your code. Something I couldn't do as easily in Emacs.

Another command I use a lot is dap, delete a paragraph, great for moving or removing function and class definitions. This is part of the text object selection feature of Vim, a quick way to select words, sentences, paragraphs and other chunks of text.

And talking about search, Vim's search and replace are very quick to grasp and easy to use; they include regular expressions as well. In the middle of writing this article I decided to refer to the newer Vim instead of the original vi and just had to run :%s/vi/Vim/g to change my document.

Configuring Vim is just a matter of adding simple assignments to the ~/.vimrc file and in most cases the default values will be enough to work. I just change the tabs based on the file's extensions, Drupal's suggestion for the number of spaces per tab is different than Python's, and add PHP syntax support for files ending in .module and .install (another Drupal tip). Finally I choose a nice color scheme, desert is my favorite these days but there are tons to choose from.

As with Emacs you can learn most of Vim using the included documentation, :h from command mode to access it, and there are a couple of nice books around: Steve Oualline's vim book (PDF, 3.7 Mb), which is free, and O'Reilly's Learning the vi and Vim editors.

And that's all for now about Vim. What can you tell us about your use of Vim? Are getting the most of it? Which are you favorite or most used commands?

In the final part of the series I'll help you find which one, Vim or Emacs, you should choose for your daily programming work.

Keep your comments relevant, written in good English and don't spam. Let's create useful and valuable discussions. Markdown is welcome.

Add your comment