How do I programmatically combine or modify the contents of Vim registers?
Answer
:let @a = @a . @b
Explanation
You can manipulate register contents directly using the :let command with the @{reg} syntax. This unlocks powerful workflows: concatenating registers, injecting separators between yanked sections, or copying a named register to the system clipboard — all without leaving the editor or resorting to macros.
How it works
:let @a = @a . @b— appends the contents of registerbto registera:let @a = @a . "\n"— appends a literal newline to registera:let @+ = @a— copies registerato the system clipboard (+register):let @a = tolower(@a)— transforms registeracontents using a VimScript function
The . operator is VimScript string concatenation. Any VimScript expression is valid on the right-hand side, including function calls, arithmetic, and other registers.
Example
You want to collect specific lines from multiple files into one paste-ready block:
" Yank a line in file 1 into register a
"ayy
" Move to file 2, yank another line into register b
"byy
" Combine them with a separator
:let @a = @a . "\n---\n" . @b
" Paste the combined result
"ap
Tips
- View all current register contents with
:regor:reg a b + - Use
:let @/ = 'pattern'to set the search register programmatically — equivalent to searching with/pattern<CR> - Assigning to
@:and@"(unnamed register) also works - This technique pairs well with
:redir @afor capturing command output into a register you then transform