Emacs: Search Text in Files

By Xah Lee. Date: . Last updated: .

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

Find Text in 1 File

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

Find Text in a Directory (without linux grep command)

Install and use the package xah-find.el

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

Find Text in a Directory (with linux grep command)

Alt+x 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: xah-find.el, Find Replace in Pure Elisp〕 )

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.

Find Text in Nested Directories

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

Display Matched Files in Dired

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

Alt+x 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, Alt+x 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).

Find Replace on Multiple Files

Emacs: Find Replace Text in Directory

Find and Replace Topic

  1. Emacs: Search / Highlight Words
  2. Emacs: Search Text in Files
  3. Emacs: Find and Replace Commands
  4. Emacs: Find Replace Text in Directory
  5. Emacs: Regex Tutorial
  6. Emacs: isearch Current Word
  7. Emacs: xah-find.el, Find Replace in Pure Elisp
Like it? Buy Xah Emacs Tutorial. Thanks.