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

How do I programmatically append text to a register without recording a macro?

Answer

:let @a .= 'text'

Explanation

:let @{reg} .= 'text' uses Vim's let command with the string concatenation assignment operator .= to append text to an existing register. This lets you build up register contents from Ex commands, scripts, or mappings without the manual uppercase-register recording trick ("Ayy).

How it works

  • :let @a = 'hello' — sets register a to 'hello'
  • :let @a .= ' world' — appends ' world' to register a; it now contains 'hello world'
  • :let @a .= "\n" . @b — appends a newline and the contents of register b to a
  • Works on any named register (az) and even special registers like "/ (search) or "+ (clipboard)
  • Read the current register contents with :echo @a

Example

Build a header comment block dynamically in a mapping:

:let @a = ''
:let @a .= "// Generated: " . strftime('%Y-%m-%d') . "\n"
:let @a .= "// Author: " . $USER . "\n"
"ap

Or append the current filename to a collection register during a macro run:

:let @l .= expand('%') . "\n"

Tips

  • :let @" .= 'text' appends to the unnamed register — the next p will include the appended text
  • Use :let @a = '' to clear a register before starting to build it up
  • This is the scripting equivalent of the manual "Ayy uppercase register append — use :let @a .= in functions and mappings, and "Ayy during interactive recording
  • :let @/ = 'pattern' sets the search register, triggering n/N to use that pattern without an explicit search

Next

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