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

50,392

Solution 1

: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

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

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

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

This script may help: Filesearch.

Share:
50,392
shibly
Author by

shibly

Updated on July 08, 2022

Comments

  • shibly
    shibly almost 2 years

    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.

  • ib.
    ib. over 12 years
    For the reference: :vim is a short name of the :vimgrep command.
  • sehe
    sehe over 12 years
    also note that :lvimgrep does the same with the location window instead of the quickfix window. Handy if you're also dealing with compiler output and don't want to go back and forth with :colder, :cnewer all the time
  • shibly
    shibly over 12 years
    but still can't get the list of filename that contains the word "JFactory".
  • Benoit
    Benoit over 12 years
    @guru: Use :copen, :cnext, :cprev and related commands to jump through search results. Reference is on :help quickfix
  • Kaleem Ullah
    Kaleem Ullah over 8 years
    how to jump to the next match, n or N are useless
  • Yi Feng Xie
    Yi Feng Xie about 7 years
    after :vimgrep, use :cw to list all of results
  • george rizk
    george rizk over 5 years
    pls put the update at the top of the comment @benoit
  • Caleb Jay
    Caleb Jay about 5 years
    Apparently that repo is deprecated as of 2016
  • mix3d
    mix3d almost 5 years
    can you explain what each of the operators do?
  • Danny
    Danny over 4 years
    Explaining the flags and command, from man grep: -H: Always print filename headers with output lines. And the -r: Recursively search subdirectories listed. the | less part will "pipe" the output into the less program, allowing you to scroll through content using the arrows, and quit by pressing q.