Working with Vim
The Tao of Vim
The underlying principle of Vim is the operator-motion pair. Think of Vim as a language, the operator and motion are the verbs and nouns that define the action on a subject.
For example, deleting a line. Delete is the action, and a line is the subject.
For Vim, d
is the delete operator, and _
is the line motion.
So, typing d_
deletes a line.
This is the essence of Vim. Instead of trying to memorize hundreds of commands, learn the verbs and nouns that make up the language, and then combine together. See operators and motions pages for more.
How they combine depends on the mode.
Vim has different modes of operations. The main modes are NORMAL, INSERT, and VISUAL mode. I capitalize the modes because vim does, I'm not shouting them. Vim's INSERT mode is like other editors, what you type is inserted into the file.
NORMAL Mode
In NORMAL mode, specify the operator first and then the motion. For example,
delete to beginning of the line: type d
for delete operator and then ^
as
the motion to the beginning of the line.
If you type ^
with no operator, the cursor moves to the beginning of the line.
if a motion is specified with no operator the cursor moves and no action
performed.
To realize the strength of Vim, you need to make NORMAL mode your default mode. I don't like telling people what to do—no vim shaming here, use the arrow keys, they're great—but the majority of power in Vim comes in NORMAL mode.
Use Esc
to return to NORMAL mode after inserting text. I mash the Esc key
often, every time I finish an edit, or make a change, I'm constantly returning
to NORMAL mode.
There is a reason it is called NORMAL mode; it is the mode where you can do all the extra vimmy stuff. INSERT mode is just typing text, any editor can do that.
Be NORMAL.
VISUAL Mode
VISUAL mode is when text is selected and visually highlighted. It may seem formal to label a mode just when text is selected, but Vim commands work different when text is selected. In VISUAL mode, the operator-motion pair is opposite NORMAL mode. Specify the motion part first by selecting, and then specify the operator for what is highlighted.
Using same example, delete to beginning of line. Type v
to enter VISUAL mode,
then type ^
as the motion to the beginning of the line, then type d
to
delete.
This is obviously an extra step to select the text, but VISUAL mode shows what is highlighted and being acted upon. Plus, you get feedback when the operator is complete since the selection is no longer highlighted.
Three different ways to enter VISUAL mode from NORMAL mode:
-
Press
v
to enter VISUAL (character) mode, any defined motion selects text by character as it moves. -
Press
V
to enter VISUAL LINE mode, the whole line is selected, any movement up or down selects by line. -
Press
ctrl-v
for VISUAL BLOCK mode, starts a block region to allow operating on columns of text, see video example below.
Psst, don't tell the purists, but if you set mouse=a
you can use your mouse to highlight things too.
VISUAL mode is pretty powerful, I use it frequently since it provides feedback seeing what is selected, and then unselected after the action. I find the feedback useful, particularly when copying.
For example, copying a whole buffer using VISUAL mode: Type gg
to move to the
top of the file, shift-v
to enter VISUAL LINE Mode, and then G
moves to
end of file selecting all the lines. With the subject highlighted, use y
to
copy.
After performing an operation in VISUAL MODE the selection is no longer
highlighted. Use gv
to automatically reselect the area previously highlighted.
Additional Modes
In total, there are seven basic modes in Vim. For completeness, the other modes are Command-line, SELECT, EX, and TERMINAL.
Command-line mode is the name of the mode entered by typing :
while in NORMAL
or VISUAL mode. This places your cursor at the bottom command-line, ready for
you to type a command.
Typically, you exit command-line mode by issuing a command, however you can exit
without a command by typing Esc Esc
A history of previous commands is kept, use the up arrow key after typing :
to
see previous commands, similar to bash. This is stored in .viminfo
so persists
between sessions.
See :help vim-modes
for full definitions on the other less used modes.