Another way that also works across files is to use :h :vimgrep with
Without the 'g' flag each line is added only once.
With 'g' every match is added.
The workflow is:
- Init your search pattern with
/your pattern, record your macro in
- Find all matching in your interested files:
vimgrep //g **/*.cpp **/*.h
- Reverse quickfix result with this command:
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
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:
Repeat it with sufficient large number, it will stop at 1st error.