11

A recursive grep in a directory should be the simplest thing, but I haven't found the recipe to get it to work yet. I'm juggling between the following commands and am not sure which has the functionality buried within it:

  • helm-find-files: The problem is this is really bad at finding a directory recursively.
  • projectile-find-dir: This is great at finding the dir! However despite using helm complete you don't wind up in helm mode, so the C-u C-s command doesn't work here.
  • helm-find: gets the recursion right but targets files, not directories
  • helm-projectile-find-dir: Seems to be what I want.
djechlin
  • 923
  • 8
  • 21
  • 1
    I love ag and ag-mode for that, the silver searcher beats grep with one hand tied to its back :-), don't know how to make it projectile aware, though – Tom Regner Apr 22 '15 at 03:33
  • M-x projectile-ag maybe ? («projectile-prefix» A or «projectile-prefix» a – depending on the version) – Mekk Apr 22 '15 at 08:25
  • 2
    To the original poster: I do not quite understand what is your purpose. projectile-ack and projectile-ag are both great recursive greps within (whole) projectile project. Aren't they what you need? – Mekk Apr 22 '15 at 08:27

6 Answers6

10

You have two ways:

  • Use helm-projectile-grep/ack/ag: You can search for everything starting from project root. Later if you want to save the search results, press F3 or press TAB to switch to action menu and select the 3rd action. To navigate hgrep buffer:

    • C-<down>: go to next match and open the match.
    • C-<up>: go to previous match and open the match.
    • M-<down>: go to next match without opening the match.
    • M-<up>: go to next match without opening the match.
    • C-o: open current match in other window.
    • RET: open current match in current window.
  • Use helm-projectile-find-dir (note the helm prefix; you must use proper Helm commands from helm-projectile package in general): narrow to a desired directory and press C-u C-s to recursively search in that directory. If you don't press C-u, it just searches in that directory without going deeper.

Hope that helps.

Tu Do
  • 6,772
  • 20
  • 39
  • 1
    You have just enabled me to unlock a huge gain by informing me of this "TAB" action within a projectile search result. Thank you. Thank you, sir. – emish May 20 '16 at 17:36
  • The tab key only makes the result open in a buffer, handy, but not what you 2 describe. Also, `helm-projectile-grep` would be perfect enough for me as is, if it would only show the *full* path, instead of only the file name. It's pretty useless if you have hierarchies with a lot of same filenames and similar content, but differ in the parent directory name – hbogert Aug 17 '23 at 08:37
6

A recursive grep in a directory should be the simplest thing

For a simple recursive grep inside current directory, just: M-x grep then, inside the minibuffer: Run grep (like this): grep -nHr "pattern". (notice the -r flag).

You may also be interested by rgrep:

rgrep is an interactive autoloaded compiled Lisp function in
`grep.el'.

(rgrep REGEXP &optional FILES DIR CONFIRM)

Recursively grep for REGEXP in FILES in directory tree rooted at DIR. The search is limited to file names matching shell pattern FILES. FILES may use abbreviations defined in grep-files-aliases', e.g. enteringch' is equivalent to `*.[ch]'.

Nsukami _
  • 6,341
  • 2
  • 22
  • 35
2

In addition to the tools mentioned in the other answers, another cool function is find-grep-dired which creates a dired buffer containing all files matching a recursive grep pattern.

It just runs find . \( -type f -exec grep -q -e my-regular-expression \{\} \; \) -ls and displays the results in a dired buffer.

2

This package has exactly what you are looking for:
https://github.com/syohex/emacs-helm-ag

The command's name is helm-do-ag.

Nek
  • 121
  • 2
1

You could try helm-do-grep. Calling it with a prefix arg gives you a recursive grep, as explained in the Helm Wiki.

If you want to launch helm-do-grep recursively without starting helm-find-files, do:

C-u helm-command-prefix-key M-g s

NOTE: If you forget to hit C-u before M-g s you can do it after file selection.

You will be prompted for selecting in which category of files to search: Use the wilcard syntax like *.el for example (search in only .el files).

By default, the extension of the file at point is used when the cursor is on a filename. If the cursor is at root of a directory, all the filename extensions found in the directory and not matching grep-find-ignored-files are inserted into the prompt.

Try it like so:

C-u helm-command-prefix-key M-g s

Manuel Uberti
  • 3,150
  • 18
  • 36
0

helm-projectile-find-dir is probably the proper functionality. You can

  1. Find a directory as in projectile-find-dir
  2. But then you finish in Helm mode where you can run C-u C-s for a recursive search as desired.
djechlin
  • 923
  • 8
  • 21