So far I always used EasyGrep for replacing text in multiple files. Unfortunately it is quite slow when a project gets bigger. One thing that seems to be amazingly fast is Ggrep of fugitive.vim that only search my version controlled files. All results are also stored in the quickfix list.
How can I use the results of Ggrep for doing a simple replace over all those found files? Is it somehow possible to use %s/foo/bar/cg
on all files in the quickfix list or are there any better ways?
Actually searching Google is pretty easy. Just type what you're interested in finding into the search box on the Google web site or into your toolbar! If you're using a toolbar, as you type, you may see words begin to appear below the toolbar's search box.
To perform a search, you'll need to navigate to a search engine in your web browser, type one or more keywords—also known as search terms—then press Enter on your keyboard. In this example, we'll search for recipes. After you run a search, you'll see a list of relevant websites that match your search terms.
Update: Vim now has cdo
, see Sid's answer.
Original Answer:
Vim has bufdo
, windo
, tabdo
and argdo
, which let you perform the same command in all open buffers, windows or files in the argument list. What we really need is something like quickfixdo
, which would invoke a command on every file in the quickfix list. Sadly, that functionality is lacking from Vim, but here's a solution by Al that provides a home-rolled solution. Using this, it would be possible to run:
:QFDo %s/foo/bar/gc
And that would run the foo/bar substitution on all files in the quickfix list.
The bufdo
, windo
, tabdo
and argdo
commands have some common behaviour. For example, if the current file can't be abandoned, then all of these commands will fail. I'm not sure if the QFDo
command referenced above follows the same conventions.
I've adapted Al's solution to create a command called Qargs
. Running this command populates the argument list with all of the files listed in the quickfix list:
command! -nargs=0 -bar Qargs execute 'args ' . QuickfixFilenames() function! QuickfixFilenames() " Building a hash ensures we get each buffer only once let buffer_numbers = {} for quickfix_item in getqflist() let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr']) endfor return join(values(buffer_numbers)) endfunction
Using this, you could follow these steps to do a project-wide search and replace:
:Ggrep findme :Qargs :argdo %s/findme/replacement/gc :argdo update
Edit: (with a hat tip to Peter Rincker)
Or you could join the last 3 commands together in a single line:
:Ggrep findme :Qargs | argdo %s/findme/replacement/gc | update
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