Macros can be repeated for every line matching a pattern with the global command as explained in this answer. This applies to every single line, but is it possible to do it for every single match? so if I save in a macro ysiw" (yank surround in word ") I repeat it for every matching regex, for example: match1\|match2 So that:
match1 nomatch match2
would become
"match1" nomatch "match2"
asked Oct 19, 2019 at 12:37
275 3 3 silver badges 11 11 bronze badges
Aside: my mnemonic for ys is « you surround »
Commented Oct 19, 2019 at 13:18
@d-ben-knoble. I like that. Thank you for the tip!
Commented Oct 20, 2019 at 21:51
Another way that also works across files is to use :h :vimgrep with g flag:
Without the 'g' flag each line is added only once. With 'g' every match is added.
The workflow is:
vimgrep //g **/*.cpp **/*.h
command! ReverseQuickFixList call setqflist(reverse(getqflist()))
This step is necessary, as there might be multiple matches in a line, the second matching position might be invalidated after you apply your macro on the 1st matching. You need to do this in reverse order, but there has no creversedo .
cdo norm! @q
Note that :h :cdo doesn't stop if an error occured in the middle. If that's a problem, jump to 1st matching with :cfirst , record a another macro:
qp@q:cnextq ^-------carriage return
Repeat it with sufficient large number, it will stop at 1st error.
1000@p