(emacs)Grep Searching

Posted: 23 March, 2008 in Technical

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 buffer of matches uses Grep
mode, which is a variant of Compilation mode (Note: Compilation Mode).

`M-x grep’

`M-x lgrep’
Run `grep’ asynchronously under Emacs, with matching lines listed
in the buffer named `*grep*’.

`M-x grep-find’
`M-x find-grep’
`M-x rgrep’
Run `grep’ via `find’, with user-specified arguments, and collect
output in the buffer named `*grep*’.

`M-x kill-grep’
Kill the running `grep’ subprocess.

To run `grep’, type `M-x grep’, then enter a command line that
specifies how to run `grep’.  Use the same arguments you would give
`grep’ when running it normally: a `grep’-style 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 tag (Note: Tags) in the buffer around point,
and puts that into the default `grep’ command.

Your command need not simply run `grep’; you can use any shell
command that produces output in the same format.  For instance, you can
chain `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 `C-x `’, <RET>, and
so forth, just like compilation errors.

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 `grep-highlight-matches’ to `t’.  When
displaying a match in the source buffer, the exact match will be
highlighted, instead of the entire source line.

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 `find’ and `grep’, so as to search
every file in a directory tree.  See also the `find-grep-dired’
command, in Note: Dired and Find.

The commands `M-x lgrep’ (local grep) and `M-x rgrep’ (recursive
grep) are more user-friendly versions of `grep’ and `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’.

These commands build the shell commands based on the variables
`grep-template’ (for `lgrep’) and `grep-find-template’ (for `rgrep’).

The files to search can use aliases defined in the variable

Subdirectories listed in the variable
`grep-find-ignored-directories’ such as those typically used by various
version control systems, like CVS and arch, are automatically skipped
by `rgrep’.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s