Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

find

vim

search

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.

like image 899
shibly Avatar asked Oct 31 '11 06:10

shibly


People also ask

How do you search for a word in all files in a directory?

Search All Files in Directory To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command.

How will you find files recursively that contains specific words in their contents?

You can use grep command or find command as follows to search all files for a string or words recursively.

How do I recursively search a folder?

An easy way to do this is to use find | egrep string . If there are too many hits, then use the -type d flag for find. Run the command at the start of the directory tree you want to search, or you will have to supply the directory as an argument to find as well. Another way to do this is to use ls -laR | egrep ^d .

What is a recursive file search?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.


1 Answers

: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.

like image 63
Benoit Avatar answered Sep 20 '22 21:09

Benoit