Saturday, May 31, 2008

Taking Advantage of Vim

Once you have mastered using Vim to replace Notepad.exe, it is time to starting taking advantage of what Vim has to offer.  Doing so can increase your productivity.  Below are most of the commands I use most frequently.  It is important to note that Vim has a veritable cornucopia of commands and thus supports many different usage styles.  What is presented here are just a fraction of the available commands.

Many commands in Vim have a similar format.  This format is [repetitions][command][movement].  For instance, 2dw will delete the next 2 words.

Useful commands that take this format include:

Command Action
c Change.  Delete the text moved over and enter insert mode.
d Delete.  Delete the text moved over.
y Yank.  Copy the text moved over.

No number implies 1.  The combination cw will delete the rest of the word under the cursor and enter insert mode.  This is probably the most commonly used combination for me.

There are two more variants on these commands.  A capital letter will execute the command to the end of the line.  D will thus delete the rest of the line.  Two repeated letters will execute the command on the entire line.  Thus cc changes the whole line and 2yy yanks the next 2 lines.

Common movement commands include:

Movement Action
w word.  A single word.
b word back.  A single word, backward.
) sentence.  A single sentence.
h left.  One character to the left.
j down.  One line below.
k up.  One line up.
l right.  One character to the right.

Movements can be issued without a command in front of them.  2w will move the cursor 2 words to the right.  You can maneuver all over the document using hjkl instead of the cursor keys if you like.  Personally, I use the cursor keys and only use l when I want to change some number of characters.  5cl will delete 5 characters and enter insert mode.

Some commands operate without a movement.  These include:

Command Action
x Delete one character
~ Change the case of one character.
r Change one letter and return to normal mode.

These commands can accept a number.  Thus 5x will delete the next 5 characters.

Other useful commands:

:<line number> Jump to that line number
% If on top of a ( or {, jump to the matching one.  Very useful in coding.
m<letter> Sets a bookmark labeled <letter> at the cursor.
'<letter> Returns to <letter> bookmark.  Useful for jumping around a file.
u Undo the last change.  Useful if you forgot you weren't in insert mode.
gg Go to the top of the file.
G Go to the bottom of the file.
>> Increase indent.
<< Decrease indent.
o Insert a new line below and enter insert mode.

These commands, in addition to the search and replace command introduced in the notepad post are the ones I find myself using most frequently.  Once you become accustomed to them, editing in something without them will feel strange.  I want to hit 2cw to change the next 2 words in Word all the time.  I find the /search very useful also and miss it whenever editing in something that does not have Vim keybindings.  My repertoire is fairly limited presently.  As I discover new ways of doing things in Vim, I'll post them as Vim tips.  If there are commands you use frequently that I don't discuss above, please mention them in the comments.

1 comment:

  1. PingBack from