Emacs: Search Text in Directory

By Xah Lee. Date: . Last updated: .

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

Find Text in a Directory (no 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).

Emacs Find Replace

  1. Search Text in Buffer
  2. isearch Current Word
  3. Isearch Whitespace Regex
  4. Highlight Text in Buffer
  5. List Delete Highlight Sort Lines
  6. Search Text in Directory
  7. Find Replace in Buffer
  8. Find Replace in Directory
  9. xah-find.el, Find Replace in Pure Elisp
  10. Regex Tutorial

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp