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

How do I copy the most recent yank into a named register without yanking again?

Answer

:let @a = getreg('0')

Explanation

When you want to preserve a valuable yank before doing destructive edits, copying register 0 into a named register is safer than re-yanking text. :let @a = getreg('0') snapshots your latest yank into register a, so later deletes and changes can churn other registers without losing that content. This is especially useful in long refactors where you want a stable paste payload.

How it works

  • Register 0 holds the most recent yank (not deletes)
  • getreg('0') reads its current contents
  • :let @a = ... writes that value into named register a
  • After this, use "ap or "aP anywhere to paste the saved text

Example

You yank a function signature with yi( and then continue editing. Before several d/c operations, save that yank:

:let @a = getreg('0')

Now even after multiple deletes, you can restore the original yank with:

"ap

Tips

  • To preserve register type (characterwise/linewise/blockwise), use setreg('a', getreg('0'), getregtype('0'))
  • Use uppercase target registers (for example A) only when you explicitly want to append

Next

How do I programmatically create a blockwise register with setreg()?