74

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
grayasm
  • 973
  • 2
  • 10
  • 14

7 Answers7

134

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

Jazz
  • 5,747
  • 5
  • 43
  • 55
  • 5
    You need grep and find (from GnuWin32), and GNU find must be before Windows find.exe in the PATH. grep and find are installed with EmacsW32. – Jazz May 06 '11 at 14:47
  • I'd also add that the `rgrep` broken a few. I.e. when I entered a pattern for a files, it found nothing. After some struggle I figured out the reason: it doesn't knows what the dots means. I.e. i wanted to search a pattern in .h, .c, .cpp files, instead of my pattern `.*\.[ch].*` I should have been entered `*\.[ch]*`. – Hi-Angel Aug 12 '14 at 10:48
14

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

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • M-x grep-find will also allow you to iterate through the results, the same way as occur. – seth Aug 26 '09 at 03:14
  • 1
    Wouldn't you need to do something like this? M-x grep "CODE_INIT_PARAMETERS" *.c It's worth mentioning that using a prefix argument will automatically set the search up to seek what the cursor is over and for the kind of file you're in. C-U M-x grep – justinhj Aug 27 '09 at 04:00
  • @justinhj, so correct. My lack of using `M-x grep` shows through. – Trey Jackson Aug 27 '09 at 05:59
  • Thanks, I quite new to emacs, and didn't know about M-x grep and igrep.el They do my job just perfect. Thanks. – grayasm Aug 27 '09 at 19:14
8

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Postow
  • 11,709
  • 17
  • 81
  • 125
6

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
Steven Huwig
  • 20,015
  • 9
  • 55
  • 79
5

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

octi
  • 1,470
  • 1
  • 17
  • 28
2

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-,).

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
1

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chmouel Boudjnah
  • 2,541
  • 3
  • 24
  • 28
  • [RipGrep performs significantly faster than Ack and Ag.. and pt and git grep and ....](https://blog.burntsushi.net/ripgrep/) – ocodo Aug 26 '22 at 14:06