Wednesday, January 26, 2011

Why, oh WHY, do those #?@! nutheads use vi?

Yes, even if you can't believe it, there are a lot fans of the 30-years-old vi editor (or its more recent, just-15-years-old, best clone & great improvement, vim).

No, they are not dinosaurs who don't want to catch up with the times - the community of vi users just keeps growing: myself, I only got started 2 years ago (after over 10 years of being a professional programmer). Friends of mine are converting today. Heck, most vi users were not even born when vi was written!

Yes, there are definite reasons why the vi/vim editing model is just superior to any other out there. And you don't need to be a Unix whiz to use it, either: vim is available for free for almost any platform out there, and there are plug-ins to get the functionality inside all major IDEs. Let's try to break a few misconceptions, and see some real examples of why it's the killerest:

Misconception #1: modal editing

The first time you stumble into vi or vim, you are shocked and disgusted that you have to use 'i' to start typing text. Can't remember which of hjkl moves in which direction. And you need to press 'a' if you want to insert after the last character of the line. Since you're already probably used to other editors, and if the arrow keys work in insert mode (they do if the system is configured properly, which is over 90% of the cases), you will stay in insert mode and not go back to normal mode except if absolutely necessary. You'll probably spend 20 minutes with it, and never go back. And also complaining: "How on earth am I going to remember whether I'm in insert or normal mode?"
Turns out, this is just a completely wrong way to use vi or vim. Using vi/vim properly, you don't use it modally. You are always in normal mode, and only enter insert mode for short bursts of typing text, after which you press <Esc> to go to normal mode. Thus, the remembering-the-mode problem just doesn't exist: you don't answer the phone in insert mode to get back to vi and not remember where you were. If you are typing text and the phone rings, you exit insert mode and then answer the phone. Or you press <Esc> when you come back. But you never think about insert mode as a mode where you stay.

Let me explain the philosophy behind this.

Commands in vi/vim are meant to be combined - 'd' means delete, 'e' means 'move to end of word', then 'de' is a complete command that deletes to the end of the current word (something like Ctrl-Shift-Right, Left, Del in most regular editors).
One good benefit of this is that the '.' command repeats the last complete, combined editing command (not movement commands). After doing 'dw', '.' will act as a command to delete to the beginning of the next word. You can move around at will with all the powerful navigation commands, and press '.' at will to delete to the next word quickly. This turns out to be incredibly powerful.
And now we come to insert commands. These commands enter insert mode and let you type text until you press <Esc>. Ok, in the case of these commands, the full command includes all the text you typed between 'i' (or whatever command you used to enter insert mode, as there are several) and <Esc>.

What this means is that if you type 'iHello<Esc>', which enters the text 'Hello' at the cursor's position, what now '.' does is "Type 'Hello' at the cursor's position". All of it. And you can understand that is pretty powerful. But it's better than this. 'A' goes to the end of the current line and enters insert mode there. So, after completing the insert by pressing <Esc>, you can press '.' anywhere to go to the end of the line and do the same thing.

Read more: ViEmu