How to repeat a change with the dot command in Vim
.cgnqa@a. repeats your last change. Make the change once, move to the next place, and press ..
A change is one command from start to finish: ciw, the new word and Esc together are one change, and . repeats all of it. Moving the cursor is not a change, so . ignores it.
Worked example
Add a semicolon to the end of each line.
A goes to the end of the line, however long it is. The dot repeats that too, so it works on lines of different lengths.
Commands
- .
- Repeat the last change.
- 3.
- Repeat it with a new count.
- cgn
- Change the next search match. . then changes the match after it.
- n.
- Go to the next match and repeat. Skip a match with n alone.
When one change is not enough: macros
The dot repeats one change. To repeat several steps, record a macro. qa starts recording into register a, and q stops. @a plays it back and @@ plays it again. Recording makes the first change, so play it only on the lines that are left.
- qa…q
- Record keys into register
a. - @a
- Play the macro.
- @@
- Play the last macro again.
- 2@a
- Play it twice.
Exceptions
- The dot repeats the change, not the movement before it. After x and w, . deletes a letter where you are now.
- Finish with Esc before you press .. In Insert mode, . just types a dot.
- A bad repeat is one change, so one u takes it back.
- Look before each repeat. A word that matches is not always a word that should change.