Emacs command for searching in files

Emacs

Emacs Problem Overview


I want to search in all files from the current folder for macro CODE_INIT_PARAMETERS.

I can do Alt + X occur, Return CODE_INIT_PARAMETERS Return, but this shows only entries from open buffers.

Is there a way to search all files from current folder, from Emacs, without switching to M-x shell and then grep? I want to avoid grep, because for some commands (M-x occur) Emacs do jumps to offending code, and I want that too.

Emacs Solutions


Solution 1 - Emacs

You can try M-x rgrep.

It will ask for:

  • the directory where you want to search recursively
  • a file pattern for the files you want to include in the search
  • the pattern you want to search

As an extra, it will exclude source control private directories from your search (like CVS, .svn or .git).

Solution 2 - Emacs

Emacs provides a built-in command:

M-x grep RET CODE_INIT_PARAMETERS *.c

(and 'grep-find to search sub directories)

Though I prefer the interface provided by an external package igrep (which provides the commands igrep and igrep-find).

Solution 3 - Emacs

If you open a folder in dired, and mark all of the files (with 'm') you can run 'dired-do-search ('A' in my bindings). This will search all marked files. To get to the next one, run tags-loop-continue (M-,)

I have set up several ELisp functions to mark various subsets of the files (.h files, .cpp files, etc.) and to create a recursive dired to search a whole tree...

Solution 4 - Emacs

This is an improvement on Trey Jackson's suggestion.

M-x grep

You will see the grep command, e.g. grep -nH -e

Add R to the first set of flags (for recursive), and put your search term after -e

grep -nHR -e CODE_INIT_PARAMETERS

Hit RET. The results will be understandable by Emacs -- you will be able to click or otherwise navigate to them, like M-x occur. You may need to put the search directory at the end of the command:

grep -nHR -e CODE_INIT_PARAMETERS /path/to/root/of/search

Solution 5 - Emacs

M-x find-grep-dired also works similarly as rgrep

Solution 6 - Emacs

In cases where

  1. you may be searching repeatedly; and
  2. etags will work

you might consider using etags and invoking either find-tag (bound to M-. by default) or tags-search (no default binding but can be continued with M-,).

Solution 7 - Emacs

There is as well ack-grep mode for Emacs which uses the ack-grep tool which is specifically designed for ''grepping'' programming languages and IMHO looks nicer than the output of M-x grep.

But as mentioned earlier etags should be the proper way!

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
QuestiongrayasmView Question on Stackoverflow
Solution 1 - EmacsJazzView Answer on Stackoverflow
Solution 2 - EmacsTrey JacksonView Answer on Stackoverflow
Solution 3 - EmacsBrian PostowView Answer on Stackoverflow
Solution 4 - EmacsSteven HuwigView Answer on Stackoverflow
Solution 5 - EmacsoctiView Answer on Stackoverflow
Solution 6 - Emacsdmckee --- ex-moderator kittenView Answer on Stackoverflow
Solution 7 - EmacsChmouel BoudjnahView Answer on Stackoverflow