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

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 register b into register a
  • :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 into a
  • :let @/ = @a — set the search pattern to whatever is in register a

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 @a displays the current contents of register a without 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 :let in mappings and functions, use the register prefix in interactive editing

Next

How do I run a search and replace only within a visually selected region?