How do I programmatically set a register's content in Vim without yanking?
Answer
:let @a = 'text'
Explanation
Vim's :let command lets you assign a value directly to any named register without performing a yank or delete operation. This is a powerful technique for pre-loading macros, combining register contents, or copying data between registers entirely from the command line.
How it works
:let @a = 'text'sets registerato the literal stringtext- The register name follows the
@sigil — any lettera–zworks - You can reference other registers on the right-hand side:
:let @a = @b— copy registerbinto registera:let @a = @a . @b— append registerbonto registera
- Set the system clipboard directly:
:let @+ = @ato push registerato the OS clipboard - Clear a register:
:let @a = ''
Example
Suppose register a holds a macro sequence and you want to prepend a step:
:let @a = 'dd' . @a
This prepends dd to whatever the macro in a currently contains — impossible with normal yank/append operations. You can also bootstrap a macro entirely from the command line before running it with @a:
:let @a = 'Iprefix: \<Esc>j'
Tips
\<Esc>and\<CR>in the string literal become the literal key codes that macros need — use\<escaping, not a literal<Esc>- Use
:echo @ato inspect a register's current contents before overwriting it - Combining
:letwith:normal @alets you build and execute macros entirely from scripts orvimrc