How do I append text to a named register programmatically without re-recording a macro?
Answer
:let @a .= "text"
Explanation
Vim registers are just strings, and you can read and write them directly using the :let command. The .= string concatenation operator lets you append to a register's current value without overwriting it. This is far more flexible than the uppercase-register append trick ("Ayy) and is essential when you need to build up register content algorithmically or add a newline separator between yanked chunks.
How it works
:let @a = "hello"— set registerato the stringhello:let @a .= " world"— appendworldto registera, making ithello world:let @a .= "\n"— append a newline character (useful before pasting multi-line content):echo @a— inspect register contents at any time
The .= operator is Vimscript's in-place string concatenation assignment, equivalent to @a = @a . " world".
Example
Suppose you want to build a register containing a multi-line snippet:
:let @a = "#!/usr/bin/env python3"
:let @a .= "\n"
:let @a .= "import sys"
Then "ap pastes:
#!/usr/bin/env python3
import sys
Tips
- Clear a register first:
:let @a = "" - Combine with
:redir: capture command output then manipulate it with:let @a .= - Use
:let @+ .= @ato append a named register into the system clipboard - Works for any register:
@"(unnamed),@+(clipboard),@/(search pattern) :put ainserts register contents onto a new line, while"appastes at the cursor