tags:

views:

202

answers:

2

How do you yank all matching lines into a buffer?

Given a file like:

match 1
skip
skip
match 2
match 3
skip

I want to be able issue a command to yank all lines that match a pattern (/^match/ for this example) into a single buffer so that I can put it into another doc, or into a summary or whatever.

The command should wind up with this in a buffer:

match 1
match 2
match 3

My first thought was to try:

:g/^match/y

But I just get the last match. This makes sense, because the :g command is effectively repeating the y for each matching line.

Perhaps there is a way to append a yank to buffer, rather than overwriting it. I couldn't find it.

+3  A: 
:help registers
:help quote_alpha

Specify a capital letter as the register name in order to append to it, like :yank A.

jleedev
Thanks for the pointer to the right part of the FM. I'll be reading it.
daotoad
+6  A: 

:g/^match/yank A

rampion
What's implicit here is that one cannot append to the unnamed buffer, at least not in a single command.You may also need to clear the buffer first: :let @a=""
J. A. Faucett