How do I recover text from older deletions using Vim's numbered registers?
Answer
"1p
Explanation
Vim automatically stores your deletion history in numbered registers "1 through "9. Every time you delete a line or more with dd, d{motion}, c{motion}, or similar, the deleted text lands in "1. The previous content of "1 shifts to "2, and so on—giving you a rolling history of your last 9 deletions to recover from at any time.
How it works
"1p— paste the most recent deletion"2p— paste the second-most-recent deletion"9p— paste the oldest deletion still in history- Only deletions spanning at least one full line (or using
D,dd,c,s) populate registers1–9; small word-level deletions withxordwgo to"-(the small-delete register) instead - Yanks (
y) do not shift numbered registers — they only update"0
Example
You delete three lines in sequence:
Delete 1: foo → stored in "1
Delete 2: bar → "1 becomes "2, bar goes to "1
Delete 3: baz → "1 becomes "2 shifts to "3, baz goes to "1
Now:
"1p " pastes: baz
"2p " pastes: bar
"3p " pastes: foo
Tips
- Use
:reg 1 2 3to inspect the current contents of registers 1–3 before pasting - Combine with
.repeat:"2pthenu"2pcycles through paste candidates without having to retype the register - In a macro,
@"replays the unnamed register — be careful not to confuse it with numbered ones - This is particularly useful after an accidental
ddearlier in a session when you've since made other deletions