This answer is based on https://stackoverflow.com/a/11976158/1057593.
However, I wanted add a few bits. Again the basic command is
:bufdo vimgrepadd pattern %
If your quickfix list is not empty, you probably want to clear it first. This can be done with :cexpr
:cexpr []
Mappings to cycle through matches
vim-unimpaired provides the mappings ]q
and [q
to cycle through the matches.
Workflow with the command line history
It is a convenient workflow to enter the search pattern separately with /pattern
. This allows you to use a generic command from your command line history using the current search pattern. This would look like
/pattern
:bufdo vimgrepadd // %
A typical new buffer wide search would be
/pattern2
:bufdo<up><up>...
I have added following mappings to my vimrc to use <C-N>
and <C-P>
instead of the arrow keys to stay with my hands on the home row
cmap <C-P> <Up>
cmap <C-N> <Down>
Note searching the command line history q:
with /bufdo<CR>
interferes with using the current search pattern. You could use :keepp /bufdo<cr>
.
For more details on the command line history see the help pages :h history
and :h cmdline-window
and https://vim.fandom.com/wiki/Using_command-line_history.
You can add to the reusable command :copen
to immediately see the quickfix list in the following way:
:execute "bufdo vimgrepadd // %" | copen
The proposal from stackoverflow :bufdo vimgrepadd // % | copen
opened several unintended windows in my case.
And with :cexpr []
the reusable command would become
:execute "cexpr [] | bufdo vimgrepadd // %" | copen