At the end of March I started to learn the Vim text editor and four months into my journey I thought it would be a useful experience for me to document the commands I have committed to memory so far. Please note that this isn’t particularly intended as an introduction to Vim or a tutorial, but if that’s what you’re looking for then there are many fine ones out there as well as some good screencasts. I’m writing this so that I have a record of what I know at this point. Think of it as a crib sheet. Also, I apologise in advance if I don’t always use the official Vim terminology, or if some of my descriptions are sloppy, but I know what I mean!
My primary editor since switching to the Mac has always been TextMate and on Windows at work I used ConTEXT, but I hadn’t particularly taken much advantage of some of the more powerful features of either editor. I decided to give Vim a go for several reasons. The main reason is that I’ve been aware of the Pragmatic Programmers’ advice to “Use a Single Editor Well” for some time now and Vim certainly fits the requirement of being “configurable, extensible and programmable”. From what I’ve seen it’s pretty much infinitely powerful, albeit with a near-infinite possible set of commands. I know that I’m just getting close enough to the surface in order to scratch it, but already I feel productive using Vim.
Another reason for picking it up is that I’m not sure who or what originated it, but there’s also been a resurgence of interest in Vim within the Ruby/Rails community recently, with many people giving Vim a try. A third reason is that Vim is ubiquitous. If there’s a platform that supports editing text files then chances are that Vim has been ported to it. Finally, I had a new person join my team at work who’s been using Vim for years and who was able to give me a flavour of what it can do and I liked what I saw.
Vim is designed to be efficient for a touch typist to use, so the general idea is to avoid using the mouse and the cursor keys. Instead, the basic four directional movement commands are on the home row, right underneath your right hand. There are probably historical reasons for this as well because Vim is an extension of vi and when vi debuted on UNIX thirty four years ago terminal keyboards didn’t even have cursor keys. So h, j, k and l move left, down, up and right respectively. Many commands in Vim can be preceded with a number representing repetition, so 10h moves the cursor ten characters to the left etc. w moves forward a word and b moves back a word. gg takes you to the top of the file and G takes you to the bottom. Pressing 0 goes to the first character of the line and ^ goes to the first non-blank character. $ goes to the end of a line. Ctrl + f goes forward one screen and Ctrl + b goes back one screen.
m<register> sets a (book)mark at the specified cursor position into the specified register, for example ma. You can return to it using `<register>. Typing :<line number> will go directly to that line. /<pattern> will search forwards for the specified text and ?<pattern> will search backwards. When performing a search, pressing n goes to the next match and N goes to the previous. Note that these are effectively reversed if searching backwards because the direction of the search is reversed. You can turn off the highlighting of the pattern matches using :nohls and turn it back on again using :set hls. Finally, % will match an opposite brace or bracket etc.
As you’d expect, Vim excels at manipulating text. i inserts text before the cursor and I inserts it before the first non-blank character in the line. a and A do the same for appending text. r replaces the character underneath the cursor with the one you type and R enters replace mode, which is like having overwrite mode turned on in other editors. x deletes the character under the cursor and dd deletes the current line whilst also yanking it to the clipboard. D deletes from the cursor position to the end of the line and C does the same thing but enters insert mode afterwards so you can start typing straightaway. dw deletes the current word and cw changes the current word with the one you type. ~ toggles the case of the selected character(s). ci" will overwrite the text within the next pair of double quotes. Ctrl + n will attempt to keyword complete the current word, moving forwards through the completion list. Ctrl + p will do the same, but moving backwards through the list. u is undo and Ctrl + r is redo.
v enables visual character selection and V enables visual line selection. y will yank a selection to the clipboard and Y will yank the current line. p pastes below and P pastes above. Similarly, o opens up a new line below and O opens up a new line above. :<range> co . will copy the lines in the specified range below the current line and :<range> mo . will move them. For example, :25,30 co . will copy lines 25 to 30 to below the current line and :25,30 mo 50 would move lines 25 to 30 to below line 50. > will indent a selection and < will outdent it. :25,30s/foo/bar/gc will replace all occurrences of foo with bar within lines 25 to 30 and prompt for confirmation of each replacement. :%s/foo/bar/g will do the same for the entire file without prompting for confirmation.
Buffers, Windows and Tabs
Vim holds text you are editing in numbered buffers, which may or may not be visible within a window. :ls will list all buffers and :bd will delete the current buffer. Switch between buffers using :bn to go to the next one in the list, :bp to go to the previous one or use <n> Ctrl + ^ to go directly to buffer n. sp will split a window horizontally and vs will split it vertically. Pressing Ctrl + w w will toggle between all open windows. Alternatively, use Ctrl + w followed by one of the motion commands. For example, Ctrl + w h will move to the window to the left of the currently active one. :only will make the current window the only one on the screen.
:tabnew will open a new tab with an empty window, or :tabe <filename> will open a new tab ready for editing the specified file. :tabclose will close the current tab. :tabn switches to the next tab and :tabp switches to the previous one, or use gt to cycle through them.
Typing . will repeat the last change. :e $MYVIMRC will edit your vimrc settings file. Entering q<register> will record typed characters into the specified register, for example qb. Press q to stop recording. Then play the macro back using @<register>. ga will display the decimal, hex and octal values for the character under the cursor. Toggle the display of hidden characters and line numbers using :set list, :set number, set nolist and set nonumber respectively. Change the current syntax highlighting colour scheme with :colorscheme <scheme>. For example, :colorscheme slate.
There are other commands that I use in Vim but I still have to look up how to use them. The ones I’ve listed above are the ones that are becoming second nature to me after four months of using Vim almost every working day. I’m pretty pleased with my progress so far and I try to learn something new in Vim every day.