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 registerato'hello':let @a .= ' world'— appends' world'to registera; it now contains'hello world':let @a .= "\n" . @b— appends a newline and the contents of registerbtoa- Works on any named register (
a–z) 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 nextpwill 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
"Ayyuppercase register append — use:let @a .=in functions and mappings, and"Ayyduring interactive recording :let @/ = 'pattern'sets the search register, triggeringn/Nto use that pattern without an explicit search