Searching in only opened buffers is nice, but there may come a time where you want to search across an entire project. It can be faster and easier than you think!
Searching across a project
grep is the Unix standard search tool. Worth mentioning due to its ubiquity. Already integrated into Vim via
:grep command. e.g.
:grep -r 'foo'. See
- Ack is faster than regular grep because it skips VCS directories, binary files, and can search for specific type of filetypes like
--perl. For Vim integration see Ack.vim
- Ag the Silver Surfer is a code-searching tool similar to ack, but faster. Vim plugin: Ag.vim
git grep searches a repository very quickly. Fugitive.vim provides
- Ripgrep is even faster than Ag and
git grep. Can be used with
:grep as well.
I recommend both ripgrep, Ag, and
git grep as they are all super fast. I have searched over 4700+ in less than a 1 second. It's pretty hard to beat ripgrep.
To test out one of these alternatives without a plugin or you simply don't want a plugin you can set your
'grepprg' option accordingly. e.g.
set grepprg=ag\ --vimgrep or
set grepprg=rg\ --vimgrep. See
:h 'grepprg' and
:h 'grepformat' for more information. Now you can search via
As a final fallback you can use
:vimgrep as it is available on all systems that Vim is available. See
:h :vimg for more help.
As most of these options use the quickfix list I suggest you use some more friendly mappings to transverse the quickfix list. I use Tim Pope's unimpaired plugin which uses
:cprev respectively. Some people like the quickfix to open after a search is performed then add the following to your
autocmd QuickFixCmdPost *grep* cwindow. You can also manually open the QuickFix list via
Want even faster "searches"?
You are going to need to use some tool that uses a prebuilt indexes like ctags, cscope, or GNU global.
- Vim has built in ctag support via
<c-]>, and many other commands and options. See
:h tags. Also may want to read Effortless Ctags with Git or look into the Gutentags plugin
- Cscope support is also built in and can be used via
:cscope command. See
I would start with this nice Vimcast episode: Search multiple files with
For more Vim help see the documentation: