I'm using Emacs for quite some time and I still somehow fail to understand what is the best way to (recursively) grep for a string within a (project) directory and get the results presented either as a dired buffer or in some even more useful way. Please enlighten me.
5 Answers
Well, since the original question doesn't mention rgrep
, I'll go ahead and point it out here. This is the simplest option already built into Emacs, and I've found it to be more than sufficient for most things.
From the documentation,
Recursively grep for REGEXP in FILES in directory tree rooted at DIR.
The search is limited to file names matching shell pattern FILES.
So, for instance, to search all python files beneath my home directory for uses ofsome_function
, I would call it with some_function
, *.py
, ~/
as the arguments. Results are displayed in a new buffer.
-
1Might want to mentioned
find-grep-dired
as well since OP mentioned "results presented either as a dired buffer or..."– npostavsCommented Aug 18, 2016 at 10:52 -
@npostavs I haven't used
find-grep-dired
myself, feel free to add it to the answer.– user2699Commented Aug 18, 2016 at 15:12 -
1I once experimented with the likes of ack and ag, on the assumption that I should be switching from rgrep to something using those, because they were supposed to be better. In the end I stayed with rgrep, because I found no benefit to changing! On the command line there's certainly a case to be made, but in Emacs
rgrep
does such a good job of targeting the search that there were no significant speed differences between them. (I want to say that rgrep was fractionally faster in some cases, but I don't remember for sure.)– philsCommented Aug 31, 2016 at 5:39 -
1Note that
next-error
andprevious-error
can be used to navigate through the result set. I findM-g M-n
andM-g M-p
the most convenient of the standard bindings.– philsCommented Aug 31, 2016 at 5:50 -
find-grep-dired
doesn't give me a buffer with cross-referencing links.rgrep
does it for me and that last suggestion aboutnext-error
previous-error
is fantastic! My only quibble is that all the messy command execution output at the start of the buffer.– robertCommented Feb 22, 2018 at 11:23
I happily use M-x find-grep-dired
for years. It returns the results as a dired buffer, which you could use.
-
I've had some difficulty with getting the cross-reference linking to work with this. It's configuration is quite nasty (
find-ls-option
) and after all that you end up with something that's quite verbose because it doesn't work without having the date before the filename!– robertCommented Feb 22, 2018 at 15:05 -
Had never heard of
fing-grep-dired
either, very neat too. Thank you!– pglpmCommented Jun 11, 2023 at 10:06
Solution 1 (best solution):
Install counsel (https://github.com/abo-abo/swiper/blob/master/counsel.el)
Then M-x counsel-git-grep
.
No setup needed (git knows the project root and files to exclude). Both git grep
and counsel
is efficient.
The project need be managed by git.
counsel requires ivy-mode.
Solution 2:
This solution uses grep and works on any project. It's inferior to the Solution 1 because it's slower and need manual setup. It's also based on ivy-mode.
(defvar simple-project-root "~/.emacs.d")
(defun simple-grep ()
(interactive)
(unless (featurep 'ivy)
(require 'ivy))
(let* ((default-directory (file-name-as-directory simple-project-root))
(keyword (read-string "Keyword:")))
(ivy-read (format "Grep at %s:" simple-project-root)
(split-string (shell-command-to-string (format "grep -rsnI %s ." keyword)) "\n" t)
:action (lambda (val)
(let* ((lst (split-string val ":"))
(linenum (string-to-number (cadr lst))))
;; open file
(find-file (car lst))
;; goto line if line number exists
(when (and linenum (> linenum 0))
(goto-char (point-min))
(forward-line (1- linenum))))))))
You need create .dir-locals.el to setup simple-project-root
, see https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html for technical details
The code in solution 2 is just a prototype. My real implementation is much more complex. See counsel-etags-grep
in https://github.com/redguardtoo/counsel-etags/blob/master/counsel-etags.el
Summary:
Those are the best two solutions I know.
If any other better solutions exist, they need at least solve below problems to be production-ready,
how to get the keyword to grep (for example, get keyword from selected region)
escape the keyword
if more efficient grep program exist, we should use it (ripgrep, the_silver_searcher/ag, ... etc), or else fallback the default grep
candidate window should use full width of screen and users can filter candidates interactively (that's why people use ivy or helm)
we should show the relative path in candidate window
able to re-use previous grepped result. So previous result should be saved. You can use
ivy-resume
fromivy
orhelm-resume
fromhelm
When you save previous grepped result, the context of previous result should also be saved. For example, in the code of of Solution 2.
default-directory
is context. See https://github.com/abo-abo/swiper/issues/591 for more details.Extended regular expression should be used because it's simpler and is already used by
counsel-git-grep
and the the_silver_searcher/ag.
Here is an example of a custom grep function that recursively greps file contents (using a regexp for the search term) in a directory and its subdirectories, and displays the results with lines of context before and after. The built-in compile.el
and grep.el
libraries provide for several methods to jump to the location in the file containing the search results. There is no need to install anything else for this example to work -- all that is needed is a full version of Emacs and a computer that has the grep
utility installed. The variable grep-program
can be adjusted to point to a particular location of the grep
utility if the default is not sufficient.
(defun recursive-grep ()
"Recursively grep file contents. `i` case insensitive; `n` print line number;
`I` ignore binary files; `E` extended regular expressions; `r` recursive"
(interactive)
(let* ((search-term (read-string "regex: "))
(search-path
(directory-file-name (expand-file-name (read-directory-name "directory: "))))
(default-directory (file-name-as-directory search-path))
(grep-command
(concat
grep-program
" "
"-inIEr --color=always -C2"
" "
search-term
" "
search-path)))
(compilation-start grep-command 'grep-mode (lambda (mode) "*grep*") nil)))
-
While the question does not seek how to simultaneously modify multiple files that are returned as results by the above grep function, I find it very helpful to use
multiple-cursors
andwgrep
. It makes modifying multiple files in one fell swoop a breeze. Said libraries, however, would need to be installed if those features are desired.– lawlistCommented Aug 17, 2016 at 17:11 -
What's the advantage of using this over the builtin
rgrep
?– npostavsCommented Aug 17, 2016 at 17:38 -
1@npostavs -- I found the built-in
rgrep
to be time-consuming to customize to my preferred regex search criteria, and I chose to hard-code everything in a custom function (which I use several times each day). If you would like to write-up an alternative answer that describes how to customizergrep
, that would be helpful to other forum readers in the future.– lawlistCommented Aug 17, 2016 at 17:41
I'd like to add another solution to @chen bin's solution which also uses counsel
(but you don't need to have a project maintained by git
for this).
You need to install ag (the silver searcher) and counsel
provides the command counsel-ag
which searches the current directory for the entered string.
If you want to search within a project(not just the current working directory), add this to your .emacs
:-
(defun rag/counsel-ag-project-at-point ()
"use counsel ag to search for the word at point in the project"
(interactive)
(counsel-ag (thing-at-point 'symbol) (projectile-project-root)))
This function will use ag
to recursively search the project root directory.
Edit: the above function defaults to the symbol at point for searching. If you do not like that behaviour, replace (thing-at-point 'symbol)
with nil
. And if you want the search results in it's own buffer press C-c C-o
Edit2: if you've ripgrep
installed, you can replace counsel-ag
with counsel-rg
in the above function and it'll just as fine :)
-
Is
projectile-project-root
useable to find Rails project root? Commented Dec 7, 2016 at 13:49 -
Yes, as long as it's a valid projectile project it will work. There was some package called projectile-rails I think. Commented Dec 7, 2016 at 14:24
-
-
It's a common style that people often use when writing their own functions(you can see it being done by a lot others if you browse their emacs config). All the functions I defined begin with
rag/
prefix and that makes them easy to find underM-x
:) Commented Dec 7, 2016 at 16:33 -
helm-projectile-grep
command (if you have helm projectile installed) orprojectile-grep
?helm
orprojectile
is, but if it is a recommended tool, I would install and learn it.projectile-grep
. Also, I HIGHLY recommend you installhelm
package. I can't imagine running emacs withouthelm
andhelm-projectile
. You might also be interesting inhelm-ag
package (which provides a helm interface for silver searcher in emacs, which is supposedly faster than grep or ack).M-x grep
doesn't do what you need. It let's you give an arbitrary command line, I sometimes usefind
there when I need the recursion.