I wrote a vim-script function, which allows me to grep a keyword in my project.
function! CustomGrep(...)
if a:0 == 0
silent vimgrep <cword> **/*.h **/*.hpp **/*.c **/*.cpp
else
silent execute 'vimgrep' a:1 '**/*.h **/*.hpp **/*.c **/*.cpp'
endif
"cope
endfunction
Now I want to write another function, which can replace a keyword:
function! CustomReplace(target, newWord)
call CustomGrep(a:target)
" loop --- how to loop?
execute 's/'.a:target.'/'.a:newWord.'/gc'
cnext
" end loop
endfunction
Obviously, I need a loop in the function, which looks like:
let cnt = CustomGrep(a:target)
for from 1 to cnt
execute 's/'.a:target.'/'.a:newWord.'/gc'
cnext
endfor
But I don't know how to get the number of the output of vimgrep
.
BTW, I know there have been some plugins, such as EasyGrep
, but I still want to make a new one.
:%s
command?:%s
only replace words in one file. What I need is to replace all occurrence in a project (a directory), just like howvimgrep
works,vimgrep
can grep all occurrence in a project recusively.:h getqflist()
or:h getloclist()
for thelvimgrep
commands. You can then work with each match:cdo
?