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

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 register b to register a
  • :let @a = @a . "\n" — appends a literal newline to register a
  • :let @+ = @a — copies register a to the system clipboard (+ register)
  • :let @a = tolower(@a) — transforms register a contents 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 :reg or :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 @a for capturing command output into a register you then transform

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?