Emacs: Find Replace Text in Directory
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.
Note: you need to have unix commands
{
grep
,
find
,
xargs
}
installed.
If not, use
Emacs: Xah Find Replace (xah-find.el) π¦
Select Target Directory
Alt+x dired
, then type a directory path, to list a directory.
γsee Emacs: File Manager, diredγ
Press ^ to go up a directory.
Now, your cursor should be on the directory you want to do the find replace.

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.
- When there are marked files, emacs will do find replace only on those marked ones.
- When there are no marked files, emacs will do find replace on the file/directory the cursor is on.
Mark Files/Directories
- m
- Mark the file/directory under cursor.
- u
- Unmark the file/directory under cursor.
- U
- Unmark all marked.

Mark Files by Regex
Alt+x dired-mark-files-regexp
γ% mγ
then type a regex. γsee Emacs: Regular Expression Syntaxγ
For example, if you want to mark all files ending in β.htmlβ, then type % m then \.html$
.
Interactive Find Replace
Alt+x dired-do-query-replace-regexp
γQγ.
Answer the prompt, by typing find regex string and replace string. γsee Emacs: Regular Expression Syntaxγ
For example, type βqueenβ Enter then βprincessβ, to replace words βqueenβ by βprincessβ.
The Result Window
Emacs will show result, in a split window.

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:
- y
- Replace current highlighted occurrence. (emacs will then jump to next one.)
- n
- Skip.
- Ctrl+g
- Abort the whole find replace.
- !
- Replace all occurrences in current file without more asking.
- N
- Skip all possible replacement for rest of the current file.
- Y
- Do the replacement on all files without further asking.
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.
Save Changed Files
Alt+x ibuffer
to list all opened files.
- Type * u to mark all unsaved files
- type S to save all marked files
- type D to close them all.
Alternative to the above options, Alt+x save-some-buffers
γCtrl+x sγ. Then emacs will display each unsaved file and ask if you want it saved.
Emacs Find Replace
- Emacs: Search Text
- Emacs: Find Replace
- Emacs: Find Replace, by Regex
- Emacs: Find Replace and Change Letter Case
- Emacs: Highlight Word, Line
- Emacs: List Matching Lines
- Emacs: Search Text in Directory
- Emacs: Find Replace Text in Directory
- Emacs: Regular Expression
- Emacs Init: isearch Whitespace Regex
- Emacs Init: isearch by Arrow Keys
- Emacs Flaw: isearch Current Word
- Emacs: Search Current Word π
- Emacs: Xah Find Replace (xah-find.el) π¦
Find Replace Scripts
- Golang: Find Text in Directory (grep) π
- Golang: Regex Find Replace Text in Directory π
- Python: Find Replace Text in Directory π
- Python: Regex Find Replace Text in Directory π
- Perl: Find Replace Text in Directory π
- Emacs: Find Replace Text in Directory
- Emacs: Xah Find Replace (xah-find.el) π¦
- Wolfram: Find Replace Text π