4

Is there some built in functionality in Vim to merge the contents of two registers into a single register by appending the two?

6

It is not exactly clear what you would like to happen when e.g. one register contains a linewise selection and the other one contains a block selection. But for the easy case, you can always do (as noted in a comment)

:let @c=@a.@b

and have the concatenation of register a and b in register c. But note, this might have funny effects, if the registers contain blockwise selections (or one register is of a different type than the other).

  • If not the registers contain blockwise selections? I'd edit, but I don't know what you meant to say. – Wildcard Sep 6 '17 at 20:58
  • This is close enough to what I wanted. I forgot to mention it in my OP, but my goal is to make it so that I can copy string after string and produce a long string consisting of all copied parts. In my case it is to yank the BibTeX keys from the bibliography file into a single LaTeX citation like so: \cite{smith2015,knuth1999,lindhe2016}. – lindhe Sep 14 '17 at 14:57
3

Let's say you want to merge "a and "b into "c:

call setreg('c', getreg('a').getreg('b'))

You can of course replace a, b and c by whatever register you want.


For a bit more of details:

getreg('x') will return the content of the register x as a string.

In vim you can concatenate two strings with . like this:

let str1 = 'foo'
let str2 = 'bar'
let result = str1 . str2
" result is equal to 'foobar'

Thus getreg('a').getreg('b') concatenate the content of registers "a and "b.

Finally setreg() allows you set the content of a register.


See the doc for more details:

  • 2
    Another way to do this would be: :let @c = @a . @b. – garyjohn Sep 6 '17 at 15:06
  • @garyjohn Ahah yes that is much easier! Using setreg became an automatism for me because I often had problems with let @... but most of the time these problems were related to the registers types so here it is not really a good argument. – statox Sep 6 '17 at 15:43
3

When registers only contain strings, we can do it with the old

:let @c = @a . @b

If you want to handle the registers as lists (one element per line), then you'll need to use the new functions described by @statox, but beware, lists are concatenated with +, not ..

:call setreg('c', getreg('a', 1, 1) + getreg('b', 1, 1))
1

Additionally and as an alternative:
if you only want to append something onto an allready filled register,
you can use the "uppercase-version" of the register-name.

For example:

  • you're marking a line of text with shift+v,
  • you'r "yanking" that text into register @a with shift+", a, y
  • next you're marking another line of text ...
  • and this one, you're appending onto register @a with shift+", A, y

As result you have two lines of text in register @a.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.