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

How do I preserve the last full-line deletion before register 1 is overwritten?

Answer

:let @a=@1

Explanation

When you delete full lines repeatedly, Vim rotates those deletions through numbered registers. The newest full-line deletion is in register 1, and it is easy to lose once another delete happens. :let @a=@1 snapshots that value into a named register so you can safely continue editing without losing it.

How it works

  • @1 reads the current contents of numbered register 1 (the most recent linewise delete/change).
  • @a is a named register you control.
  • :let @a=@1 copies the text, so later deletes can rotate numbered registers without touching your backup.

This is especially useful during refactors where you are cutting many lines and occasionally need to reinsert one earlier deletion. Instead of interrupting your flow to paste immediately, you can cache it in a and keep moving.

Example

Suppose you deleted this block a moment ago, and it currently lives in register 1:

if retries > 3:
    alert_ops()

Run:

:let @a=@1

Now keep deleting other lines. Later, paste the preserved block with:

"ap

Tips

  • Use uppercase to append: :let @A=@1 appends into register a.
  • Inspect before pasting with :reg a.
  • This works for any source/destination register pair, not only 1 -> a.

Next

How do I reindent the previous visual selection and keep it selected?