Continuing from what we learned in part 1 (installing and vimtutor), I’d now like to focus on the fundamental commands in Vim.
Commands
Vim commands are formed from a combination of verbs and targets. The targets could be objects (words, sentences, paragraphs, lines, the contents of parentheses) or movements (jump to end of word, jump to end of paragraph, jump forward until the letter ‘e’, etc). Forming objects generally involves the use of a modifier. You can also add a count to perform the action count times.
Verbs
Here’s some example verbs (don’t try and learn them all at once!):
i
- insert - enter insert modea
- append - enter insert mode after the caratI
- enter insert mode at the beginning of the lineA
- append to line (enter insert mode at the end of the line)o
- open a line after the current one and enter insert modeO
- open a line before the current one and enter insert moded
[target] - delete (remove from the document and put in buffer)y
[target] - yank (copy)p
- paste the buffer after the cursorP
- paste the buffer before the cursorc
[target] - change (delete and then enter insert mode)r
[char] - replace the character under the cursor with [char]x
- delete the character under the cursoru
- undo the last commandCtrl-R
- redo the last undo (sidenote: in vim undo/redo forms a tree, changes aren’t lost)/
- enter regex search moden
- find the next instance of the search termN
- find the previous instance of the search term.
- repeat last change (extremely powerful)
Nouns/Movements
Nouns or movements are commands for moving within the document or representing an area within a document. Common movements are:
h
,j
,k
,l
- equivalent to the arrow keys left, down, up, right - you should aim to use these rather than the arrow keys, but don’t worry too much yet!0
- move to the very beginning of the current line^
- move to the first non-whitespace character on the line$
- move to the end of the linew
,b
- move to the next/previous wordW
,B
- asw
/b
only Words are bigger)
,(
- move to the next/previous sentence}
,{
- move to the next/previous paragraphf
/F
[char] - find the next/previous instance of [char] in the current linet
/T
[char] - until - find the character before the next/after the previous instance of [char]/
[regexp]- like t
but instead of finding a character it finds a regexp%
- jump to the matching parenthesis (vim understands nested parenthesis)_
- move to the current line (useful for making commands line-based)
Trying it out
Try some of these movements out now - you can start to imagine how much faster document navigation will be once you’ve mastered them.
Once you’ve got a feel for the movements, try combining them with verbs.
Don’t forget that if you do something wrong (or don’t understand what
happened) you can simply press u
to undo, and try it again. For example:
cw
- “change” until the end of the current “word”d}
- delete until the next paragraph (note that this is line-based delete, so it will delete the whole of the current line too)d_
- delete the current line (this can also be entered asdd
for brevity)y_
- yank the current line (this can also be entered asyy
for brevity)p
/P
- paste the previously yanked (or deleted) text. If the yank (or delete) was character based then it will paste after/before the cursor, otherwise it will paste on the next/previous line.27x
- delete the next 27 characters
Counts
You can generally precede a verb or a movement with a count, which will perform that action that many times. For example:
3w
move to the third word from here3j
move the cursor three lines down3}
move to the 3rd paragraph from here3i
will write whatever you insert 3 times. (The duplication will not occur until you exit insert mode, e.g. with<Esc>
.)c3w
will change the next 3 words (i.e. it will delete 3 words and then enter insert mode).3dd
will delete the current line and the two below it3p
will paste the contents of the buffer three times
Modifiers
Modifiers can be used between a verb and a noun to modify its meaning.
For example, let’s look at i
[noun] (inner/inside):
ciw
- change the whole word the cursor is on, even if the cursor is in the middle of the wordci)
- change the contents of the current pair of brackets (understands bracket pairing)ci"
- change the contents of the current pair of doublequotes (understands quote escaping)
another modifier is a
- around - like i
but includes the container.
Next time
In my next post, I’ll talk about some of the ways I’ve used these basic commands to speed up my editing.