How do I cycle through my previous deletions to find the one I want to paste?
Answer
"1p then u.u.u.
Explanation
Vim stores your last 9 deletions (of one line or more) in the numbered registers "1 through "9. Register "1 holds the most recent delete, "2 the one before that, and so on. The trick is that after pasting from "1p, each subsequent undo-and-dot (u.) advances to the next numbered register, letting you cycle through your delete history until you find the text you need.
How it works
- Every time you delete or change one or more lines, the text shifts through the numbered registers like a queue
"1always holds the most recent delete,"2holds the previous one, etc."1ppastes the most recent delete- Pressing
uundoes that paste - Pressing
.repeats the put command but automatically increments the register number — so it pastes from"2instead - Repeat
u.to try"3,"4, and so on, up to"9
This auto-increment behavior is a special property of the numbered registers and the dot command — it does not work with named registers.
Example
Suppose you deleted three lines at different points during your editing session:
- Deleted
third deletion(now in"1) - Before that, deleted
second deletion(now in"2) - Before that, deleted
first deletion(now in"3)
You want to recover first deletion but you're not sure which register it landed in. Type "1p to paste third deletion. Not the one you want — press u. to try "2 which pastes second deletion. Still not right — press u. again to try "3 which pastes first deletion. That's the one! Leave it in place.
Tips
- Use
:reg 1 2 3 4 5 6 7 8 9to preview all numbered registers without cycling - Small deletes (less than one line, like
dworx) go into the"-(small delete) register instead of the numbered registers - The numbered registers are shared across all buffers in a Vim session
- Yanks do not shift through numbered registers — only deletes and changes do. Yanks go to
"0 - If you know the exact register, skip the cycling:
"3ppastes directly from register 3 - This feature essentially gives you a 9-level undo history for deletions, separate from Vim's undo tree