How do I cycle through previously deleted text using numbered registers?
Answer
"1p then u then . to cycle
Explanation
Vim stores your last 9 deletions in numbered registers "1 through "9. Register "1 holds the most recent delete, "2 the one before that, and so on. By combining paste, undo, and the dot command, you can cycle through your delete history to find and recover text you deleted earlier.
How it works
"1ppastes the most recent deletion (same as the unnamed register for deletes)- Press
uto undo that paste - Press
.(dot) to repeat with the next numbered register ("2p, then"3p, etc.) - Each
.automatically increments the register number - Stop when you find the text you want
Example
You deleted three lines at different times:
Delete 1: "first important line"
Delete 2: "second important line"
Delete 3: "third important line" (most recent)
To find and paste "first important line":
"1p " pastes "third important line"
u " undo
. " pastes "second important line" (from "2)
u " undo
. " pastes "first important line" (from "3) - found it!
Tips
- Only deletions of one line or more go into numbered registers; smaller deletes go to the small delete register
"- - Use
:registers 1-9to see all numbered registers at once - The
"0register is separate: it always holds the last yank, not affected by deletes - Numbered registers shift down with each new delete:
"1becomes"2, etc.