Vim Undo, Redo, and Repeat — u, Ctrl+r, .
Learn Vim's undo tree, redo, and the powerful dot command for repeating changes. Understand u, Ctrl+r, and the . command that experienced Vim users rely on constantly.
Detailed Explanation
Undo and Redo
| Command | Action |
|---|---|
u |
Undo the last change |
Ctrl+r |
Redo (undo the undo) |
U |
Undo all changes on the current line |
{n}u |
Undo the last n changes |
Vim's Undo Tree
Unlike most editors that have a linear undo history, Vim maintains an undo tree. If you undo several changes and then make a new edit, the previous redo history is not lost — it becomes a branch in the undo tree. You can navigate this tree with:
g-— Go to an older text stateg+— Go to a newer text state:earlier 5m— Go to the text state from 5 minutes ago:later 10s— Go to the text state 10 seconds forward
The Dot Command (.)
The . command repeats the last change. This is arguably Vim's most powerful feature:
Example workflow:
ciwfoo— Change the word under cursor to "foo"- Move to another word
.— Change that word to "foo" too
What counts as a "change":
- Everything from entering Insert mode to leaving it
- Any Normal mode command that modifies text (
x,dd,>>, etc.) - A substitution command
Combining Dot with Search
The killer pattern is combining . with n:
/oldWord— Search for the wordcwNewWord— Change itn— Jump to next match.— Repeat the change- Repeat steps 3-4 as needed
This gives you manual control over each replacement, which :%s does not.
Use Case
You need to undo mistakes, redo changes, or apply the same edit to multiple places in a file using Vim's dot repeat command for efficient repetitive editing.