Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim yank all matches of regex group into register

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

:g/regex/y/A

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 &)
like image 287
user2506293 Avatar asked May 17 '14 16:05

user2506293


1 Answers

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=''
like image 188
FDinoff Avatar answered Nov 14 '22 20:11

FDinoff