How do I copy the contents of one register into another without pasting?
Answer
:let @a = @b
Explanation
:let @{reg} = @{reg} copies the contents of one register into another using Vim's variable assignment syntax. Registers are accessed as @{letter} variables in Vimscript, so you can read, write, copy, and transform them with :let — no pasting or yanking required.
How it works
:let @a = @b— copy registerbinto registera:let @+ = @"— copy the unnamed register to the system clipboard:let @" = @+— copy the system clipboard to the unnamed register:let @a = @0— copy the yank register intoa:let @/ = @a— set the search pattern to whatever is in registera
Practical examples
Save the clipboard to a named register before overwriting it:
:let @z = @+
" ... do work that changes the clipboard ...
:let @+ = @z
Copy a macro from one register to another:
:let @b = @a
Now @b replays the same macro as @a.
Build a search from yanked text:
yiw
:let @/ = @0
n
Search for the word you just yanked.
Tips
:echo @adisplays the current contents of registerawithout pasting- You can transform while copying:
:let @a = toupper(@b)or:let @a = substitute(@b, 'old', 'new', 'g') :let @" = ''clears the unnamed register- This is the scripting equivalent of
"ay/"ap— use:letin mappings and functions, use the register prefix in interactive editing