Emacs: Search Text in Files (grep)

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page is a tutorial on using Emacs to search text.

Some of these commands are written entirely in emacs lisp, some work as a wrapper to the unix {grep, find} commands.

The following are the most often used emacs text-searching related commands.

List Lines Matching a String, for a Single File

Call list-matching-linesAlt+s o】. It'll list all lines in the current file that matches a given text. Here's a sample output:

emacs list-matching-lines occur 2014
emacs list-matching-lines (alias of occur)

Clicking the line will jump to the match location in the file.

If you want to match with word boundary, use \b, like this \bcat\b. This will find “cat” but not “cats”. 〔➤ Emacs: Regex Tutorial

There's also delete-matching-lines and delete-non-matching-lines. 〔➤ Emacs: List/Delete/Highlight Matching Lines, Sort/Reverse Lines

Find Text in a Directory

Call grep to list all files in the current folder. The current folder is usually the folder the current opend file is in. (You can first call dired to a folder you want.) It will prompt you like this:

grep --color -nH -e ▮

For example, if you give this input grep -nH -e "Cheshire" *html, which will list all files ending in “html” that contains the word “Cheshire”.

If you want case insensitive search, add a -i in the grep option.

emacs grep 2014
emacs grep

On Microsoft Windows, the matched texts are not highlighted as of . To highlight it, call highlight-phrase.

Find Text in Nested Directories

You can also use grep-find. “grep-find” is similar to {grep, rgrep, lgrep}. It uses a combination of unix's grep and find. It will prompt you with this:

find . -type f -print0 | xargs -0 -e grep -nH -e MySearchStr▮

Just type your search string at the cursor position. (the exact command prompted may be slightly different depending on your OS.)

Display Matched Files in Dired

How to have the unix “find” result shown in dired?

Call find-dired.

Explanation: In unix, there's a very useful command that list files in a directory with a particular property. For example, if you want to list all files ending in “.html” in a directory and all subdirectories under it, you can do find . -name "*.html" -print.

However, the result is a textual output. Let's say you want to run a word count “wc” command on all such files. You can use “find”'s option “-exec”, or in combination with “xargs” command. For example: find . -name "*.html" -print | xargs -l -i wc {}.

However, sometimes you want to do several complicated things with this set of files, and you want to do them interactively. For example, some of such files you want to word count, some of them you want to run another command on, and some of them you need to rename, and what to do depends on the previous commands. In this case, it will be useful, to have this list of files shown in emacs's dired mode, then you can use all emacs dired power to manipulate these files.

Stepping Thru Files of Matching Text in Dired

While in dired, you can step thru all files that matches a text pattern. In dired, call dired-do-searchA】. It will prompt you for a search string, then open the first file of the match, with your cursor placed at the matching location.

To go to next occurrence, press 【Meta+,】 (tags-loop-continue).

Calling Linux Shell Commands from Emacs

Emacs can call Linux shell commands, such as {grep, find}. You can call shell commands by shell or shell-command. 〔➤ Emacs Shell Tutorial

On Microsoft Windows, you can install unix commands by cygwin. 〔➤ Installing Cygwin Tutorial

Find/Replace on Multiple Files

Emacs: Interactively Find/Replace Text in Directory

Like it? Buy Xah Emacs Tutorial.