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:
- Init your search pattern with
/your pattern
, record your macro in q
.
- 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 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:cnext<cr>q
^-------carriage return
Repeat it with sufficient large number, it will stop at 1st error.
1000@p
ys
is « you surround »