Emacs: Find Replace Text in Directory

By Xah Lee. Date: . Last updated: .

Suppose you want to do find and replace, for hundreds of files in a directory, either in one shot, or asking yes/no for each occurrence.

Here's the solution.

Select Target Directory

Alt+x dired, then type a directory path, to list a directory.

Press ^ to go up a directory.

Now, your cursor should be on the directory you want to do the find/replace.

emacs dired find replace dir 2017 06 24
emacs dired

Select Only Some Files

If you want to find/replace only files ending in “.html”, or “.js”, or other selections of files, you need to mark files.

Mark Files/Directories

emacs dired marked files 2017 06 d830f
emacs dired marked files

Mark Files by Regex

Alt+x dired-mark-files-regexp% m

then type your regex pattern.

For example, if you want to mark all files ending in “.html”, then type 【% m】 then \.html$.

[see Emacs: Regex Tutorial]

Interactive Find Replace

Alt+x dired-do-query-replace-regexpQ】.

Answer the prompt, by typing find regex string and replace string.

For example, type “queen” Enter then “princess”, to replace words “queen” by “princess”.

[see Emacs: Regex Tutorial]

The Result Window

Emacs will show result, in a split window.

emacs dired do query replace regexp 2017 06 96227
emacs dired-do-query-replace-regexp result. Note the prompt at bottom.

The top pane, is a file where a match is found. Cursor is place on the word.

The bottom pane shows a list of files where match is found. This buffer is name “*xref*”.

In the top pane showing the actual file, the following keys are available:

If you want to cancel the whole operation without saving any changes you've made, type 【Ctrl+g】, then exit emacs.

Result Overview: xref buffer

The bottom pane buffer named “*xref*”, lists all the occurrences and files.

Move cursor to the “*xref*” pane, then the following keys are available:

Enter → Display the reference on the current line.

n or . → Move to the next reference and display it in the other window (xref-next-line).

p or , → Move to the previous reference and display it in the other window (xref-prev-line).

Ctrl+o → Display the reference on the current line in the other window (xref-show-location-at-point).

r → prompt for find replace with regex. (xref-query-replace-in-results)

q → Quit the window showing the *xref* buffer. (quit-window)

In the xref buffer, Alt+x describe-mode to see the doc.

(info "(emacs) Xref Commands")

Save Changed Files

Alt+x ibuffer to list all opened files.

  1. Type 【* u】 to mark all unsaved files
  2. type S to save all marked files
  3. type D to close them all.

Alternative to the above options, Alt+x save-some-buffersCtrl+x s】. Then emacs will display each unsaved file and ask if you want it saved.

Alternative, xah-find.el

Emacs requires the unix grep command installed. On Microsoft Windows, you need to install it.

Alternatively, try

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

It does not require grep, is easier to use, and provides other features.

Dired topics

  1. File Manager, dired
  2. Dired Customization
  3. Find Replace Text in Directory
  4. Batch Rename Files
  5. Dired Sort File by Time, Size
  6. Open File in External App
  7. Rename File from Space to Hyphen/Underscore
  8. Convert Image Files in Dired

Find Replace Topic

  1. Search / Highlight Word
  2. Search Text in Files
  3. Find and Replace Commands
  4. Find Replace Text in Directory
  5. Regex Tutorial
  6. isearch Current Word
  7. List / Delete / Highlight / Sort Lines
  8. xah-find.el, Find Replace in Pure Elisp

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