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

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
  • "1 always holds the most recent delete, "2 holds the previous one, etc.
  • "1p pastes the most recent delete
  • Pressing u undoes that paste
  • Pressing . repeats the put command but automatically increments the register number — so it pastes from "2 instead
  • 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:

  1. Deleted third deletion (now in "1)
  2. Before that, deleted second deletion (now in "2)
  3. 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 9 to preview all numbered registers without cycling
  • Small deletes (less than one line, like dw or x) 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: "3p pastes directly from register 3
  • This feature essentially gives you a 9-level undo history for deletions, separate from Vim's undo tree

Next

How do I edit multiple lines at once using multiple cursors in Vim?