Easy and Powerful Multi-Buffer and Multi-File Text Searching in Vim
Here are some enhancements that I recently made to my Vim configuration that have revolutionized its power and ease-of-use when grepping for text across multiple buffers and files.
- Install the grep plug-in.
- Add the following to your "
~/.vimrc":function! QFixToggle(forced) if exists("g:qfix_win") && a:forced == 0 cclose else execute "copen" endif endfunction " Used to track the quickfix window. augroup QFixToggle autocmd! autocmd BufWinEnter quickfix let g:qfix_win = bufnr("$") autocmd BufWinLeave * if exists("g:qfix_win") && expand("<abuf>") == g:qfix_win | unlet! g:qfix_win | endif augroup END " '\g' : grep all open buffers :noremap <Leader>g <Esc>:GrepBuffer <CR> " '\gg' : grep all open buffers for word under cursor :noremap <Leader>gg <Esc>:GrepBuffer <C-R><C-W><CR> " '\G' : recursively grep through filesystem :noremap <Leader>G <Esc>:Rgrep<CR> " '\qq' : toggle QuickFix window (errors and vimgrep results here) noremap <silent><leader>qq <Esc>:call QFixToggle(0)<CR> " '[q' previous quickfix entry map [q :cprev<CR> " ']q' next quickfix entry map ]q :cnext<CR> " '\q*' : search for occurrences of word under cursor, and write to QuickFix noremap <silent><leader>q* <Esc>:execute 'vimgrep '.expand('<cword>').' '.expand('%') <CR> :copen <CR> :cc
And that's all it takes!
Now, position your cursor on any word, and type "\gg". All lines where that word occurs across all open buffers will be added to your QuickFix buffer. Typing "[q" or "]q" will cycle you forward and backward through those occurrences.
You can also type "\g", and then enter a word or regular expression to search for an arbitrary pattern across all open buffers.
If you type "\G", you can then search for a word or regular expression in any part of your filesystem, recursing down through sub-directories starting from a location that you specify, and filtering files by glob pattern.
In all cases, the QuickFix buffer will be populated with the search results (filenames and line numbers), and you can visit each of the results using the "[q" or "]q" keys.
feed
Comments
0 comments postedPost new comment