24 votes

How to recursively search / grep file contents in a directory / subdirectories in Emacs?

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 ...
user2699's user avatar
  • 2,241
17 votes

How to recursively search / grep file contents in a directory / subdirectories in Emacs?

I happily use M-x find-grep-dired for years. It returns the results as a dired buffer, which you could use.
Michael Albinus's user avatar
10 votes
Accepted

Is it possible to remove matches manually in grep mode?

Absolutely. And it is extremely useful to do that. M-x flush-lines and M-x keep-lines are your friends, as well as just C-k etc. (The first thing you need to do is of course C-x C-q, to make the ...
Drew's user avatar
  • 77.5k
8 votes

rgrep : visiting matches without switching buffer

By default C-o does exactly what you want in grep buffers as well as in occur buffers. You can also simply navigate up and down inside the grep results buffer with p and n (previous-error-no-select, ...
phils's user avatar
  • 51.2k
6 votes

How to recursively search / grep file contents in a directory / subdirectories in Emacs?

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). ...
chen bin's user avatar
  • 4,867
6 votes

How to grep marked files in the dired mode of emacs?

If you use library Dired+ (dired+.el) then you can use command dired-do-grep (bound by default to M-g in Dired mode) to do what you request. diredp-do-grep is an interactive compiled Lisp function ...
Drew's user avatar
  • 77.5k
6 votes

emacs grep eliminate suspects

You can always make the *grep* buffer writable with C-x C-q and delete lines the normal way (e.g. with C-a C-k C-k). When you are done, press C-x C-q again to make it read-only if you wish and use TAB ...
NickD's user avatar
  • 29.8k
6 votes
Accepted

Filter text of buffer in a grep-like way

I want the text not matching the pattern to be gone M-x keep-lines will delete lines not matching the pattern (i.e. only keep the lines which match). flush-lines does the opposite, deleting lines ...
phils's user avatar
  • 51.2k
5 votes
Accepted

Advanced git grep from within emacs

I found the answer to my own question. It seems M-x grep is pretty powerful, in that it can handle any "grep like" shell command's output. So you can do M-x grep # it will ask you for the command, ...
american-ninja-warrior's user avatar
5 votes
Accepted

Search files by content using full boolean expressions

Try using the UNIX or GNU/Linux find command. It supports Boolean operations. In Emacs you can, for example, use command find-dired, which runs find and puts the set of found files in a Dired buffer....
Drew's user avatar
  • 77.5k
5 votes
Accepted

Grep pipe support and how to filter grep results

Note that the first grep grep --color=always -nH -e "text_to_find" ~/path/to/files/*.org puts escape sequences around matches for text_to_find to colorize those matches. If the second search string ...
Tobias's user avatar
  • 33.2k
4 votes

How to recursively search / grep file contents in a directory / subdirectories in Emacs?

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 ...
Chakravarthy Raghunandan's user avatar
4 votes

How to recursively search / grep file contents in a directory / subdirectories in Emacs?

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 ...
lawlist's user avatar
  • 19.1k
4 votes

How to grep marked files in the dired mode of emacs?

(defun dired-grep-marked-files () "`i` case insensitive; `n` print line number; `I` ignore binary files; `E` extended regular expressions." (interactive) (let* ((files (dired-get-marked-files)) ...
lawlist's user avatar
  • 19.1k
4 votes
Accepted

Jump to file and line number in grep file

The grep parameters you are using should be adequate. Just do M-x grep-mode after opening your results file and you should get the links you desire. You could also skip writing the grep results to a ...
ebpa's user avatar
  • 7,479
4 votes

Grep pipe support and how to filter grep results

What you were attempting with a pipe works fine for me (tested in 26.1 and 25.3). Which version are you using? Does your command work outside of Emacs? Edit: C-hv grep-use-null-device might mess up ...
phils's user avatar
  • 51.2k
4 votes
Accepted

How to keep grep results in eshell buffer

eshell has several built-in commands (grep among them) and it prefers to use the built-in version: ~ $ which grep eshell/grep is a compiled Lisp function in ‘em-unix.el’. As the manual that I linked ...
NickD's user avatar
  • 29.8k
3 votes

How can I create a new buffer for the second run of grep, to avoid overwriting the results of the first run of grep?

M-x rename-buffer. Use M-n to retrieve the current name, and edit it. E.g., M-n puts *grep*in the minibuffer. Type 1 to change it to *grep*1, etc. Then just use M-x grep again, to have the new ...
Drew's user avatar
  • 77.5k
3 votes
Accepted

How to grep marked files in the dired mode of emacs?

Eshell's grep is compatible with grep(1) and uses Emacs's internal grep interface, thus you can try something like the following: M-x dired-do-eshell-command RET grep -nH --color your-search--pattern ...
xuchunyang's user avatar
  • 14.5k
3 votes

Is there a command in emacs similar to `find` in findutils?

find-dired will run find for you.
George Moutsopoulos's user avatar
3 votes

Multiple grep results buffers

You can do this easily if you use library Grep+ (grep+.el). Use + in your first *grep* buffer, followed by g. The + renames the first *grep* buffer by appending <N> (N=2,3,...) to it, and it ...
Drew's user avatar
  • 77.5k
3 votes
Accepted

How to grep all the Info manuals with a regexp search

The following solution relies upon three (3) command-line utilities: find; xargs; and zgrep. In putting together the following function, I discovered that grep cannot see inside gizipped files, and ...
lawlist's user avatar
  • 19.1k
3 votes
Accepted

How to align grep output?

Here is the complete solution: (setcar (car grep-regexp-alist) "^\\(.+?\\)\\(:[ \t]*\\)\\([1-9][0-9]*\\)[ \t]*\\2") (defun eab/grep-align () (interactive) (read-only-mode -1) (toggle-...
artscan's user avatar
  • 445
3 votes
Accepted

find-grep-dired: find exited abnormally with code 1

You need to use cygwin's or msys find command. The error messages shown above seem to come from the windows find command that is incompatible with emacs. See "Using grep' andfind' on MS Windows" on ...
p_wiersig's user avatar
  • 1,071
3 votes

color rgrep matching results in the rgrep results buffer

grep (rgrep) uses two settings (that I know of) to control highlighting in the *grep* buffer font-lock-mode grep-highlight-matches - customization variable grep-highlight-matches depends on font-...
sshaw's user avatar
  • 200
2 votes

Recursive grep in directory with helm and/or projectile?

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's user avatar
  • 121
2 votes

Show the matching line in the center of the screen instead of the bottom when opening a grep result

Displaying the source line in the middle of the window is the default behaviour, so something in your own config has caused your problem. The variable C-hv next-error-recenter usually determines ...
phils's user avatar
  • 51.2k
2 votes

Advanced git grep from within emacs

This feature has been recently added to Emacs (see this commit), and will probably be out on version 27. The NEWS entry is: *** New customizable variable 'vc-git-grep-template'. This new ...
Federico's user avatar
  • 207
2 votes

Unable to visit file from grep results

This is an emacs bug in emacs 26.1, resolved in 26.2. It has to do with —null being added to the default grep arguments, so the drive letter in Windows paths, with its colon, was not matched by the ...
InHarmsWay's user avatar
  • 1,319

Only top scored, non community-wiki answers of a minimum length are eligible