vim yank all matches of regex group into register

I know that I can yank all matched lines into register A like this:


But I can't seem to figure out how to yank match regex groups into register A:

:g/\(regex\)/\1y A
(E10: \ should be followed by /, ? or &)


  • You can do this with a substitute command.

    :%s/regex/\=setreg('A', submatch(0))/n

    This will append register a to whatever the regex matched. The n flag will run the command in a sandbox so nothing will actually get replaced but the side effects of the statement will happen.

    You probably want to empty the register first with

    :let @a=''