I'm not sure if I've failed at searching for the answer to this, but now I've worked it out I wanted to document it here. The problem is searching for a multiline pattern, and storing all such occurrences in a register.
I am trying to look for calls to a particular function, e.g. func(a, b, c);
, so that I can extract them to a separate file. I'm doing this in Vim (rather than using, say, sed or awk), because I find Vim's searching over multiple lines syntax easiest.
If I search for a pattern, I know I can store the line(s) containing the results in a register:
:let @a=''
:g/func\s*([^;]*;/y A
Stores all the single line calls to func
in @a
.
Unfortunately, this doesn't quite work if I adapt this search pattern to find multiple line calls:
/func\s*(\_.\{-\};
matches multi-line calls such as:
func(a, b,
c,
d);
-- no matter how many new lines/parameters are in the way, but the corresponding global command :g/func\s*(\_.\{-\};/y A
just stores the first line of each match. I can sort of understand why this happens (y
yanks the line the result is on, and doesn't know it takes multiple lines).
How can I adapt the command to store all lines of each occurrence in a register?