Just as you can run a compiler from Emacs and then visit the lines
with compilation errors, you can also run
grep and then
visit the lines on which matches were found. This works by treating
the matches reported by
grep as if they were errors.
The output buffer uses Grep mode, which is a variant of Compilation
mode (see Compilation Mode).
grep asynchronously under Emacs, listing matching lines in
the buffer named *grep*.
find, and collect output in the
zgrep and collect output in the *grep* buffer.
Kill the running
grep, type M-x grep, then enter a command line
that specifies how to run
grep. Use the same arguments you
grep when running it normally: a
regexp (usually in single-quotes to quote the shell’s special
characters) followed by file names, which may use wildcards. If you
specify a prefix argument for M-x grep, it finds the identifier
(see Xref) in the buffer around point, and puts that into the
Your command need not simply run
grep; you can use any shell
command that produces output in the same format. For instance, you
grep commands, like this:
grep -nH -e foo *.el | grep bar | grep toto
The output from
grep goes in the *grep* buffer. You
can find the corresponding lines in the original files using M-g M-n, RET, and so forth, just like compilation errors.
See Compilation Mode, for detailed description of commands and key
bindings available in the *grep* buffer.
Some grep programs accept a ‘--color’ option to output special
markers around matches for the purpose of highlighting. You can make
use of this feature by setting
t. When displaying a match in the source buffer, the exact
match will be highlighted, instead of the entire source line.
As with compilation commands (see Compilation), while the grep command runs, the mode line shows the running number of matches found and highlighted so far.
grep commands will offer to save buffers before
running. This is controlled by the
The possible values are either
nil (don’t save),
(ask before saving), or a function which will be used as a predicate
(and is called with the file name as the parameter and should return
nil if the buffer is to be saved). Any other
nil value means that all buffers should be saved without
asking. The default is
The command M-x grep-find (also available as M-x
find-grep) is similar to M-x grep, but it supplies a different
initial default for the command—one that runs both
grep, so as to search every file in a directory tree. See also
find-grep-dired command, in Dired and Find.
The commands M-x lgrep (local grep) and M-x rgrep
(recursive grep) are more user-friendly versions of
grep-find, which prompt separately for the regular expression
to match, the files to search, and the base directory for the search.
Case sensitivity of the search is controlled by the current value of
case-fold-search. The command M-x zrgrep is similar to
M-x rgrep, but it calls
zgrep instead of
grep to search the contents of gzipped files.
These commands build the shell commands based on the variables
rgrep). The files to search can use aliases defined in
Directories listed in the variable
grep-find-ignored-directories are automatically skipped by
M-x rgrep. The default value includes the data directories used
by various version control systems.
By default, the shell commands constructed for
zgrep are abbreviated for display by
concealing the part that contains a long list of files and directories
to ignore. You can reveal the concealed part by clicking on the
button with ellipsis, which represents them. You can also
interactively toggle viewing the concealed part by typing M-x
grep-find-toggle-abbreviation. To disable this abbreviation of the
shell commands, customize the option
grep-find-abbreviate to a