How do I browse and restore previously deleted text using Vim's numbered registers?
Answer
"1p then u. to cycle through delete history
Explanation
Vim silently maintains a rolling history of your last 9 deletions in numbered registers "1 through "9. Most users know about "1p, but the real power is in how Vim rotates through them: after pasting from "1p, you can press u. repeatedly to cycle forward through "2p, "3p, and so on — recovering text you deleted much earlier in the session.
How it works
- Whenever you delete a chunk of text (with
d,c,s, orxon ≥1 line), Vim stores it in"1and shifts the previous contents of"1→"2,"2→"3, up to"9(which is discarded) "1ppastes the most recent deletionuundoes the paste, and.re-does it — but Vim's special rotation rule means.now refers to"2pinstead of"1p- Repeating
u.walks backward through your entire deletion history
Example
Suppose you deleted three lines at different points in your session. To recover the third-most-recent deletion:
"1p → paste most recent deletion
u → undo it
. → paste from "2 (second-most-recent)
u → undo it
. → paste from "3 (third-most-recent)
You can also paste directly: "3p for the third deletion, "9p for the oldest stored.
Tips
- Only deletions of at least one full line use numbered registers; character-level deletes with
xgo to the small-delete register"-instead - Use
:registers 1 2 3to inspect the current contents of the first three numbered registers before choosing - Single-character
xdeletions do not fill"1; usedlinstead if you want the deletion tracked