vimtricks.wiki Concise Vim tricks, one at a time.

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, or x on ≥1 line), Vim stores it in "1 and shifts the previous contents of "1"2, "2"3, up to "9 (which is discarded)
  • "1p pastes the most recent deletion
  • u undoes the paste, and . re-does it — but Vim's special rotation rule means . now refers to "2p instead 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 x go to the small-delete register "- instead
  • Use :registers 1 2 3 to inspect the current contents of the first three numbered registers before choosing
  • Single-character x deletions do not fill "1; use dl instead if you want the deletion tracked

Next

How do I browse the full commit history of the current file using vim-fugitive?