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 c
:g/^match/yank A
This runs the global command to yank any line that matches ^match
and put it in register a
. Because a
is uppercase, instead of just setting the register to the value, it will append to it. Since the global command run the command against all matching lines, as a result you will get all lines appended to each other.
What this means is that you probably want to reset the register to an empty string before starting: :let @a=""
.
And naturally, you can use the same with any named register.
:help registers
:help quote_alpha
Specify a capital letter as the register name in order to append to it, like :yank A
.
Oh I just realized after commenting above that it's easy to yank matching lines into a temporary buffer...
:r !grep "pattern" file.txt
The simplest solutions come once you've given up on finding them. :)