0

I have a quite large directory that I frequently need to search through for specific lines. Currently I simply use the built in grep:

:vim "search" **/*.cpp | cw

The problem is that going through all files in the repo takes ~1-2 minutes normally due to having to the OS globbing all files, and it really slows down development. On the command line, I can store file paths in a file and use them without having to reindex everytime:

> find . -type f -name "*.cpp" > cppfiles
> grep "search" `cat cppfiles` # possible to reuse the index files for multiple searches. Goes _a lot_ faster.

Can I do the same thing with the built-in search?

1

On unix like systems, you can use backticks directly in the vimgrep command (in any command that takes file names)

:vimgrep "search" `cat cppfiles` | cw

There is vim-only way by loading the cpp files into the argument list:

:%argdelete
:argadd **/*.cpp
:vimgrep "search" ##

## is a special notation for the argument list.

  • I thought I tried this, but alas. However, it turns out that this is still comparatively slow -- I think I'll just resort to grepping in the terminal. Thanks! – pingul Dec 11 '17 at 21:15
  • You may also want to use vim's :grep instead, which uses grepprg (by default the program "grep"). It is generally faster than :vimgrep. – Mass Dec 11 '17 at 21:43
  • Oh wow -- :grep is a lot faster! Do you know how I can use it in a good fashion? Currently I use the command command! -nargs Grep :vimgrep <q-args> 'cat hcppfiles' | cw, but changing it to grep does not display the search results as neatly (I can't jump to the different files, for example). – pingul Dec 11 '17 at 21:57
  • Related Vimcasts episode: Search multiple files with :vimgrep. I also would recommend use :grep. Even better use a faster grep, like The Silver Searcher or ripgrep. However it looks like you are using C/C++ which means you can use ctags and/or cscope. Which will be a precomputed database so can jump to definitions instantly. See :h tags & :h cscope. – Peter Rincker Dec 12 '17 at 15:36
  • @pingul It should work by default. After :grep you should see the entries in the quickfix window (doing :cw) just like with vimgrep. Make sure grepprg and grepformat are set sensibly (defaults ought to work). – Mass Dec 12 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.