How can I search a word in whole project/folder recursively?

SearchVimFind

Search Problem Overview


Suppose I'm searching a class JFactory inside a folder and it's sub-directories.

How can I file that file which contains class JFactory?

I don't want to replace that word but I need to find that file that contains class JFactory.

Search Solutions


Solution 1 - Search

:vimgrep /JFactory/ **/*.java

You can replace the pattern /JFactory/ with /\<JFactory\>/ if you want full word match. :vim is shorthand for :vimgrep.

If JFactory or \<JFactory\> is your current search pattern (for example you have hit * on one occurrence) you can use an empty search pattern: :vimgrep // **/*.java, it will use last search pattern instead. Handy!

Warning: :vimgrep will trigger autocmds if enabled. This can slow down the search. If you don't want that you can do:

:noautocmd vimgrep /\<JFactory\>/ **/*.java

which will be quicker. But: it won't trigger syntax highlighting or open gz files ungzipped, etc.

Note that if you want an external program to grep your pattern you can do something like the following:

:set grepprg=ack
:grep --java JFactory

Ack is a Perl-written alternative to grep. Note that then, you will have to switch to Perl regexes.

Once the command of your choice returned, you can browse the search results with those commands described in the Vim documentation at :help quickfix. Lookup :cfirst, :cnext, :cprevious, :cnfile, etc.

2014 update: there are now new ways to do that with the_silver_searcher or the_platinum_searcher and either ag.vim or unite.vim plugins.

Solution 2 - Search

From the project root folder, run following:

grep -H -r 'what_you_search' * | less

You will get a list of folders and matching lines with that string.

Solution 3 - Search

The Silver Searcher(https://github.com/ggreer/the_silver_searcher) highly recommended, really fast!

install

sudo pacman -S the_silver_searcher  // arch linux 
sudo apt install silversearcher-ag  // ubuntu

usage

$ ag keywords

integrate with vim

rking/ag.vim (https://github.com/rking/ag.vim)

after installing

:Ag keywords

Solution 4 - Search

Take a look at ctags and cscope which let you jump to class and function definitions, and find where those functions/classes are used.

Solution 5 - Search

This script may help: Filesearch.

Solution 6 - Search

Open the command line window by:

Esc - to enssure you are in Normal mode

type q , type :

the command line should open ( it like a tmp file to write the command you can navigate as you would navigate normally in any vim file ...

type i to enter insert mode

this example will search for the to_srch string recursively bellow the current dir for all file types of type '.js' and '.java' but omit all file paths containing the string node_modules

:g/console.log/ | :vimgrep /console.log/ `find . -type f -name '*.js' -o -name '*.java' -not -path '*node_modules/*'`

Now wheen you :copen you could navigate with the arrow keys through the sarch results ...

you could also set those in .vimrc

	" how-to search recursively under the current dir for the files of type js and java but omit the
	" node_modules file paths
	":g/console.log/ | :vimgrep /console.log/ `find . -type f -name '*.js' -o -name '*.java' -not -path '*node_modules/*'`
	" reminder open the quick fix window by :copen 20
	" reminder close the quick fix window by :ccl

you could omit the first :q/to_srch/ I use it to highlight the search results automatically since I have "set hlsearch" in my ~/.vimrc

Any hint how-to enable automatically the srch results from the vimgrep or in vimrc will be highly appreciated ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshiblyView Question on Stackoverflow
Solution 1 - SearchBenoitView Answer on Stackoverflow
Solution 2 - SearchKarlo SmidView Answer on Stackoverflow
Solution 3 - SearchAnthony ZhanView Answer on Stackoverflow
Solution 4 - SearchKenView Answer on Stackoverflow
Solution 5 - SearchJeetView Answer on Stackoverflow
Solution 6 - SearchYordan GeorgievView Answer on Stackoverflow