The Vim Command Pattern




Vim at first seems to be full of magic keystroke sequences you have to memorize.

You can run on memory alone, but it will limit your learning and frustrate you eventually.

Do not memorize a bunch of key sequences, but instead learn to understand how commands fit together.


Most of the time you will either get an immediate result from a keystroke, or you will type a command and a movement command (often repeating the same keystroke: the "double-jump").

When you start to learn the other bits and pieces (registers, repeats, etc) then you might think vim is inconsistent, but this is not so.  The command pattern is rather consistent, but some parts of the command are optional.
  • Register name (optional, with default cut/paste register used if not otherwise specified)
  • Repeats (optional): 13
  • Operation: y (for yank)
  • Movement (depending on the operation): yy (repeated to take current line, a convention used in vi)
VIM commands work with the pattern shown above. There are some commands that don't use register and some that don't take movement, but for the most part this is the way it goes.

A register is essentially a cut-n-paste buffer. In most editors you get only one. In VIM you have too many, but you don't have to use them, so don't worry about it untl you get to the lesson on buffers.

A repeat is a number of times you want to do something. If you don't type in a number, the default is to do it only one time.

An operation is a keystroke that tells VIM to do something. These are mostly normal keypresses, and most operators do not require shifts or alts or controls.


Movement is a command that takes the cursor somewhere. There are a lot of them, because there are lots of ways you need to move. Don't panic, though, because you can use the arrow keys if you really have to. There is a whole section of this tutor on moving around.

Lets try an example to clarify how the pattern works. If I want to copy 13 lines into my copy/paste register, I can skip specifying a register name, type 13 for a repeat count, press 'y' for yank, and then press one more 'y' as a movement command (meaning current line). That yanks 13 lines into the default cut-n-paste register. If I press 'p' (choosing to use no register name and no repeat, recognizing that put has no movement command), then those lines are pasted back into my document just after my current line.

If you know this pattern, then you will know how to leverage everything else you learn about vi. Learn all the convenient ways to move, and you will know how to cut, paste, reformat, shift, and many other things you want to do.

No comments:

Post a Comment