Emacs: Search Text in Files (grep)

By Xah Lee. Date: . Last updated: .

This page shows you how to find text in a file or directory.

Single File

For single file/buffer, you can search or highlight by:

Find Text in a Directory

Call grep to list all matching text of all files in the current directory.

(Emacs's grep command calls the unix grep command. On Microsoft Windows, you need to install cygwin, or use xah-find.el. See: Emacs: Find/Replace in Pure Elisp xah-find.el.)

The current directory 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 output, showing list of files with matched text.

For a pure emacs lisp solution without the unix grep, see: Emacs: Find/Replace in Pure Elisp xah-find.el

Find Text in Nested Directories

Find text in directory and all sub-directories, you can call any of:

Search Text Without Unix grep

See: Emacs: Find/Replace in Pure Elisp xah-find.el

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. 〔➤see Emacs Shell Tutorial

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

Find/Replace on Multiple Files

Emacs: Interactively Find/Replace Text in Directory

Like it? Buy Xah Emacs Tutorial. Thanks.