search/grep over all open buffers

2 Points

Phil Shaughnessy Phil Shaughnessy

1 year ago

Frequently I want to find or move between usages of a particular word or term across all the files I'm working with.

This lets you grep/vimgrep across all the open/saved buffers, which populates the quickfix list. The first mapping lets you type the search, the second executes the search for the word under the cursor.

How does this work?

bufdo does the command iteratively over each buffer. Because of bufdo, we need to incrementally build up the quickfix list, which is why we use vimgrepadd instead of vimgrep (this also works with grep). Because we are appending, we need to clear the quickfix list - this is :cex []<CR>.

nnoremap <leader>s :cex []<CR> :silent bufdo vimgrepadd // % <Left><Left><Left>
nnoremap <leader>a :cex []<CR> :silent bufdo vimgrepadd /<C-r><C-w>/ %<CR>