1

I want to grep contents of all CMakeLists.txt files in a project (nested within a directory) so I can jump from each instance, like I would when running emacs grep function. In the examples below I'm looking for "Boost".

I can get the results I want with the following command in bash:

grep Boost $(find . -name CMakeLists.txt)

I tried:

  • in eshell calling $grep Boost $(find . -name CmakeLists.txt)
  • calling emacs-grep with grep --color -nH --null -e Boost $(find . -name CMakeLists.txt)
  • noodling around with projectile.

So far with no success. Can someone enlighten me?

4
  • In the eshell and emacs-grep invocation, did you forget the .txt or is that a typo?
    – NickD
    Oct 1, 2021 at 15:33
  • indeed, a typo.
    – Spacemoose
    Oct 1, 2021 at 16:11
  • FWIW, using emacs-grep with a similar command worked fine for me.
    – NickD
    Oct 1, 2021 at 17:42
  • See also C-h f lgrep.
    – Y. E.
    Oct 2, 2021 at 9:22

2 Answers 2

0

I'm not sure I understand the question, so maybe this won't help, but...

There's command find-grep-dired. Here's its doc from the version in find-dired+.el:

find-grep-dired is an interactive compiled Lisp function in find-dired+.el.

It is bound to menu-bar search find find-grep-dired, menu-bar subdir find find-grep-dired.

(find-grep-dired DIR REGEXP &optional DEPTH-LIMITS EXCLUDED-PATHS)

Use Dired on the list of files in DIR whose contents match REGEXP.

The find command run (after changing into DIR) is essentially this, where LS-SWITCHES is (car find-ls-option):

find . \( -type f -exec grep grep-program find-grep-options \
  -e REGEXP {} \; \) LS-SWITCHES

Thus REGEXP can also contain additional grep options.

Optional arg DEPTH-LIMITS is a list (MIN-DEPTH MAX-DEPTH) of the minimum and maximum depths. If nil, search directory tree under DIR.

Optional arg EXCLUDED-PATHS is a list of strings that match paths to exclude from the search. If nil, search all directories.

When both optional args are non-nil, the find command run is this:

find .
 -mindepth MIN-DEPTH -maxdepth MAX-DEPTH
  \( -path EXCLUDE1 -o -path EXCLUDE2 ... \)
  -prune -o -exec grep-program find-grep-options -e REGEXP {} \;
  LS-SWITCHES

where EXCLUDE1, EXCLUDE2... are the EXCLUDED-PATHS, but shell-quoted.


There's also command find-dired, which is essentially a general find command:

find-dired is an interactive compiled Lisp function in find-dired+.el.

(find-dired DIR ARGS &optional DEPTH-LIMITS EXCLUDED-PATHS)

Run find and put its output in a buffer in Dired Mode.

Then run find-dired-hook and dired-after-readin-hook.

The find command run (after changing into DIR) is essentially this, where LS-SWITCHES is (car find-ls-option):

find . \( ARGS \) LS-SWITCHES

Optional args:

  • DEPTH-LIMITS: Minimum and maximum depths: (MIN-DEPTH MAX-DEPTH).

  • EXCLUDED-PATHS: Strings matching paths to be excluded. Uses find switch -path.

When both optional args are non-nil, the find command run is this:

find . -mindepth MIN-DEPTH -maxdepth MAX-DEPTH
       \( -path EXCLUDE1 -o -path EXCLUDE2 ... \)
       -prune -o \( ARGS \) LS-SWITCHES

where EXCLUDE1, EXCLUDE2... are the EXCLUDED-PATHS, but shell-quoted.

0

If I'm reading the question rightly, you want M-x find-grep

You would run that and then edit the template, adding the files names to search, and the pattern to grep. The before/after would be something like this:

find . -type f -exec grep --color -nH --null -e  \{\} +
find . -type f -name CmakeLists.txt -exec grep --color -nH --null -e 'Boost' \{\} +
               ^^^^^^^^^^^^^^^^^^^^                                  ^^^^^^^

You would enter the grep pattern first, as Emacs puts the cursor at that position in the prompt (after the -e); but you can then edit the rest of the options as required. By default, if you enter only a grep pattern, the command will search all regular files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.