Vim — Editing with Verbs and Motions
Anyone can open Vim, type some text and quit. What makes it fast is Normal mode, where short key combinations delete, copy and change whole words or lines without touching the mouse. They are easy to remember, because they're built like tiny sentences.
The rule
Verb then motion: d plus a move deletes that far.
A verb says what to do:
| Verb | Action |
|---|---|
d |
delete (the text is kept so you can paste it) |
y |
yank, which means copy |
c |
change: delete, then drop into Insert mode |
A motion says how far to go. These are the movement keys you already know: w (to the next word), $ (to the end of the line), G (to the end of the file).
Put them together:
dwdeletes to the start of the next word.d$deletes to the end of the line.cwchanges the rest of the current word.yGcopies everything from here to the end of the file.
Typing the verb twice acts on the whole line: dd deletes a line and yy copies one. A number in front repeats: 3dd deletes three lines and 2w jumps two words.
The rest of the everyday toolkit:
| Key | Does |
|---|---|
x |
delete the character under the cursor |
p / P |
paste after / before the cursor |
u |
undo |
Ctrl+r |
redo |
. |
repeat the last change |
/word |
search forward for "word"; n finds the next match, N the previous one |
And one command-line trick: :%s/old/new/g replaces every "old" with "new" in the whole file.
The shortcut
Say what you want out loud as "verb, how far", and the keys follow. For example, "delete three lines" is 3dd, "change to end of line" is c$, and "copy this line" is yy.
Find the line
She types
/basiland presses Enter. The cursor jumps to "Pick basil every day every day".
A common slip is pressing u over and over and undoing further than you meant. Ctrl+r redoes one step at a time, so nothing is lost.
Drills
Answer fast — trust the rule.
Which keys delete the current line?
What does y stand for in Vim?
You want to replace the rest of a word with new text. Which command?
You just deleted a line with dd. What does pressing . do next?
Which command replaces every "tomatos" with "tomatoes" across the whole file?
Remember
- Verb then motion:
d,yorcfollowed by a move acts that far. Double the verb for a whole line, and put a number in front to repeat. - Say it as "verb, how far" and the keys follow.
uundoes,Ctrl+rredoes, and.repeats your last change.
