I am looking for an elegant solution to (within vim script) iterate over all matches of a regular expression in a buffer. That would be something like
fu! DoSpecialThingsWithMatchedLines()
for matched_line_no in (GetMatchedPositions("/foo\\>.*\\<bar"))
let line = getline(matched_line_no)
call DoItPlease(line)
end for
endfu
Is there something like this? I am not necessarily looking for a full fledged solution, any pointer directing me into the right direction will do.
Thanks / Rene
Most of the time I'd use michael's solution with :global
You can also play with filter(getline('1','$'), 'v:val =~ "foo\\>.*\\<bar"')
if you really want to use :for
.
Otherwise, you can simply call search()
in a loop.
EDIT: 9 years later, in scripts, I'd now use map()
+ filter()
:call map(filter(getline('1','$'), 'v:val =~ "foo\\>.*\\<bar"'), 'DoItPlease(v:val)')
This:
:for
based solutions, If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With