In particular, I'd like to append to the unnamed register with the :global command.


There's no shortcut to append only to the default register (the way you can append to a letter register by using the uppercase register name.)

In order to append only to the default register, you'd have to use a full let @" .= ... expression, such as:

:g/pattern/let @" .= getline('.')."\n"

But that's quite unsatisfying. It's much better to use a letter register instead, and then use the uppercase version of the register to append to it.

Writing to a letter register will also write to the unnamed register (thanks @Mass for pointing that out!). From [:help quote_quote]:

Vim fills this register with text deleted with the d, c, s, x commands or copied with the yank y command, regardless of whether or not a specific register was used (e.g. "xdd). This is like the unnamed register is pointing to the last used register. Thus when appending using an uppercase register name, the unnamed register contains the same text as the named register.

So we can just use a named register as a "proxy" to the unnamed register. We can initialize it with the initial contents of the unnamed register, then append to it, which will get the unnamed register to include the appends:

:call setreg('x', @", getregtype('"'))
:g/pattern/y X

While you'll be primarily appending to register "x, the unnamed register will also get the same contents, mirroring the named register you last appended to.

  • 1
    Maybe not the spirit of the question but, :g/^/y A does "append to the unnamed" register after the first line – Mass May 16 '20 at 23:16
  • @Mass It actually does... Why is it that this works and appends these lines to the default register other than the "a register? – filbranden May 16 '20 at 23:23
  • 3
    Well, the trick is that when you perform any yank (except to _), the unnamed register is updated to point to that register (your answer is totally correct). – Mass May 16 '20 at 23:44
  • 1
    @Mass Are you sure ? That's the same as my example, I just tried it again, blah is always put. – dedowsdi May 17 '20 at 12:00
  • 1
    @dedowsdi, sorry, not sure what was or I imagined happening, but I can't reproduce any more with --clean. – Mass May 17 '20 at 12:06

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.