Don’t remember exactly how, but somehow I stumbled upon ViEmu, a plug-in for Visual Studio that makes its code editor support Vi/Vim-style editing. For uninitiated, the point of Vi is to dramatically reduce the number of hand movements and keystrokes required to move around and change the code. It does this by defining shortcuts that automate typical move and edit operations (sort of like familiar Ctrl+Arrow that moves cursor to the next delimiter or Ctrl+I that starts incremental search but WAY more various and advanced). The presumption is that once one learns all the shortcuts, s/he can edit the code without ever moving hands to the cursor keys or to the mouse. This is especially cool on notebooks, where both the cursor keys and the mouse usually suck.
It’s funny how, as I was taking my first steps in ViEmu, my first thought was “hell, this is cool but once computers know exactly which line and position I’m looking at I’ll no longer need Vi”. We shall see if this is an overstatement or not in the next 50 years.
Anyway, this post is a recap of commands in the sequence that I learned them. Let’s jump right in.
hjkl move cursor correspondingly left, down, up, and right. While left and right are easy to remember, up and down seem reversed, mostly because down feels logically similar to right (as it, too, moves cursor towards the end of the document). The mnemonics to remember j for down is “j looks like an arrow pointing down” which of course is a tough analogy to buy. Anyway, I guess I’ll have to struggle with up and down until the motor memory kicks in. According to Vi theory, if you find yourself moving around with hjkl a lot, you’re not using Vi properly. Instead you should learn to use more powerful “moves” — i.e. commands that smartly move the cursor exactly to the location you want it to be at, instead of going there space by space.
i switches the editor to the Insert before mode — the newly typed text will appear immediately in front of the character that the cursor was on when i was pressed.
a switches the editor to the Append mode, or insert After mode — which is the Insert mode starting at the position immediately after the current character.
As many other commands in Vi, i and a have their upper case counterparts — I and A — that work in a consistently different way: I goes to Insert at the very left of the current line and A goes to Append at the very end of it. Two slightly more advanced relatives of I/A are o and O which go into Insert/Append after Opening a new line, either before (O) or after (o) the current line.
Esc quits the Insert mode and gets us back to the Command mode, also called Edit mode, which is the mode we should supposedly be in 90% of the time.
In Insert mode there are no commands, the text you type is what gets typed. In Edit mode, all you type gets interpreted as a command, which may or may not change the text.
Somewhat confusing is the fact that Edit mode is indicated by a block cursor which, back in terminal days, used to indicate the write-over mode. In Vi, the write-over mode (also called Replace mode) is turned on by pressing capital R, which does not seem terribly useful except for rare cases where the new text is exactly same length as the old text, which is more likely to happen for very short lexems, 1-2 chars long. Think replacing delimiters, variable names, and array indexes.
b and e move the cursor word-by-word, similarly to how Ctrl+Arrows normally do. The definition of word is a little different though, as several consecutive delmiters are treated as one word. If you start at the middle of a word, pressing b will always get you to the Beginning of the current word, and each consecutive b will jump to the Beginning of the next word. Similarly, and easy to remember, e gets the cursor to the End of the current, and each subsequent, word.
similar to b/e, capital B and E move the cursor word-by-word using only whitespaces as delimiters.
Another word-by-word move is w (and W) which is sort of a hybrid between b and e, in the sense that it moves cursor to the beginning of the next Word, instead of the end of this and each subsequent one, as e does.
x deletes the character next to the cursor, exactly like Delete key does, and X is equivalent to Backspace.
u is Undo, plain and simple.
capital D (take a deep breath) Deletes the rest of the line to the right of the cursor, same as Ctrl+Shift+End/Del in normal editors (notice 2 keypresses — Shift+D — instead of 4)
capital J very effectively Joins the current line with the next one with one space symbol in between. Once done, it places the cursor between the two parts of the combined line, on the newly added space.
That’s it for now, this should jump-start your ViEming experiments, and remember that if you feel frustarted and need something done quickly you can always switch to Insert mode by pressing i which enables normal Ctrl/Shift/Cursor keys and basic clipboard shortcuts. If worse comes to worse you can even turn the Vi mode off by pressing Ctrl+Shift+Alt+V, but I hope it won’t get to that. Instead I recommend creating a cheat sheet and using it for a day or two so you can quickly impress your friends with your new ninja editing skills. Happy Viing!
EDIT — also see second part in this series here: http://zvolkov.com/blog/post/2009/04/18/Learning-Vi-part-2.aspx