On Vi and why I like it

It has been pointed out to me that the content of late on this site has been a little off-the-wall. It has, but that is one of the small prerogatives that I have as sole owner, writer and proprietor of this site. So, for one post at least, back to our normal technological showings.

Vi has something of a reputation as a program you either love or hate. Given the people I have spoken to, this reputation has been gained via the simple exponent of being true. People either think Vi — that’s vee-eye pronunciation-watchers, though I am guilty of using a single syllable version — people either think Vi is the best editor since sliced bread or that it is an esoteric piece of coding that only the most crazy among us could enjoy using.

I started to use Vim, or Gvim to be precise, around a year ago. Prior to this I used nedit almost exclusively. Nedit is a fine piece of software, there is no doubt. It is one of the few editors that I have found that allow you to use regular expressions to define syntax-highlighting, rather than just highlighting given keywords. This meant that syntax errors were very easy to spot. If the line didn’t highlight, it was very likely to be wrong. The regexp, whilst not being true syntax awareness, was close enough for most uses. I still use nedit for many things; mainly when I am going to want nice cut-and-paste with the rest of the desktop, as Vim is sometimes a little strange in this.

I decided to look at Vim because I had heard so much that made it sound like just the editor for me. Fast, efficient coding with a minimalist interface. I had tried emacs for a while when I started university. For all its power, emacs always felt a little slow and clunky. In addition the shortcut key-sequences seemed to force my hands to carry out very unnatural feats of contortion. More than one key press for an operation such as Save seemed a little excessive to me.

I really started using Vim after I installed Gentoo and began to use Linux full-time. I had bought myself the O’Reilly essential tomes, Running Linux and Linux in a Nutshell. I was reading through the sections on emacs and Vim and thought to myself, I’ve tried emacs and didn’t really get on with it, so I should try Vim.

Vim is very much a hackers program: take the time to learn it and it will repay you ten fold. Like many commands and programs that date from before GUIs were widespread, it is designed for lightening fast use via the keyboard. Just like the ls command presents a far more useful default output than the verbose and convoluted dir program in DOS, Vi was designed by a hacker for a hacker. It is esoteric and it will seem like only crazy people would ever want to use it by their own choosing.

Vim has two modes: command mode and insert mode. In each mode, different things happen in response to key presses. In insert mode, key presses do what they usually do and the words you type appear within your document. In command mode, however, things operate somewhat differently. You use command mode to execute command, such as save and quit, but you cannot input text in command mode. Thus Vi is different to most editors that only have one mode and rely on key combinations to execute commands, whereas many Vi commands just take one key press.

The most useful commands are those to do directly with editing. There are tens of commands and hundreds of ways of combining them. Fortunately, once you know the commands, the combining works in the same way all the time. Simple commands move the cursor about. These can be combined with a number to move a given number of lines or characters. For example, in command mode, typing the characters 10j will move you ten characters to the left. There are special characters for end of line and so on that you can use. This makes moving around the document a breeze.

There are further commands for deleting text, which can be combined in similar ways; e.g. 10dd inserts ten lines of text. Actually it cuts them, and the command p pastes them where you need them. This simple combination is perhaps my most used. I’m forever rearranging my code and this takes around two seconds to move a hundred lines. Contrast this with a mouse-driven method where you have to select all the text and you can see one reason why I say that Vi makes editing far faster than other editors. Moving around the document and text within the document, two of the most common things I do when coding, is made exponentially faster than a non-modal editor.

In addition to this, Vi has extensive search capabilities. I’ve only really scratched the surface of these. In command mode /searchword[enter] does a search. Again, far faster than using a dialog. To repeat the search, just use /[enter]. You can also do regular expression searches, which come in very useful every once in a while. Search and replace are also excellent. Search and replace can also be combined with the movement commands in a similar method to the above examples to allow you to replace a given item over a ten line stretch, rather than a whole file. Having two modes allows normal keys to be used to execute commands, rather than the Fn keys or combinations, so your fingers never have to move far.

The other thing worth mentioning about Vi is that it is almost entirely internally consistent. To insert ten lines, you do pretty much the same as search and replace over ten lines, which is similar to move down ten lines and so forth. This mean that once the initial fairly steep learning curve is surmounted later learning all seems to click into place. Learn a command and you know just how to apply it to any given situation as it will work the same as all the other commands you know.

Once you know how to use Vi, it really does Just Work. If you are looking for just and editor, I don’t think you can do much better than to invest a week or two of your time to learn Vi.

.:.