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

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 register a to the literal string text
  • The register name follows the @ sigil — any letter az works
  • You can reference other registers on the right-hand side:
    • :let @a = @b — copy register b into register a
    • :let @a = @a . @b — append register b onto register a
  • Set the system clipboard directly: :let @+ = @a to push register a to 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 @a to inspect a register's current contents before overwriting it
  • Combining :let with :normal @a lets you build and execute macros entirely from scripts or vimrc

Next

How do I use capture groups in Vim substitutions to rearrange or swap matched text?