Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to find words in multiples files with Vim

Tags:

vim

I use Vim for coding for years, mostly with NerdTree to handle easily a big project with multiple files - like a MVC framework.

But i faced regulary a issue, how to find words, sentences, keywords in files into the project? All the files aren't open in buffers. I used to make it with this find & grep command in the terminal but it's need me to put vim in hte background, copy the name of the files and go back to vim.

find . -type f -name *.js -print0 | xargs -0 grep -i foo

Not really userfriendly...

Is there a better/faster way to do this ?

like image 426
Xavier Avatar asked Nov 01 '25 05:11

Xavier


2 Answers

There are different ways to do that:

  • you can use :vimgrep to do grep
  • you can call external grep
  • you can install vim ack plugin: https://github.com/mileszs/ack.vim (I am using this one)

with :vim or ack plugin, the found file path+name would be listed in quickfix list, to let you jump to easily.

also I have written a script to do "grep" again on quickfix list, may helpful too:

https://github.com/sk1418/QFGrep

like image 58
Kent Avatar answered Nov 04 '25 02:11

Kent


You can run external commands using r:

:r! command-to-run

You could use your existing find command with this:

:r! find . -type f -name *.js -print0 | xargs -0 grep -i foo

The output of running this command will be inserted into your current buffer.

like image 24
Derecho Avatar answered Nov 04 '25 03:11

Derecho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!