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.
Select Target Directory
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.
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.
- Press m to mark the file/directory under cursor.
- Press u to unmark the file/directory under cursor.
- Press U to unmark all marked.
Mark Files by Regex
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
[see Emacs: Regex Tutorial]
Interactive Find Replace
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.
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:
- Type y to replace current highlighted occurrence. (emacs will then jump to next one.)
- Type n to skip.
- Type Ctrl+g to abort the whole find/replace.
- Type ! to replace all occurrences in current file without more asking.
- Type N to skip all possible replacement for rest of the current file.
- Type Y to 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
p or , → Move to the previous reference and display it in the other window
Ctrl+o → Display the reference on the current line in the other window
r → prompt for find replace with regex.
q → Quit the window showing the *xref* buffer.
In the xref buffer, Alt+x
describe-mode to see the doc.
(info "(emacs) Xref Commands")
Save Changed Files
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 requires the unix
grep command installed. On Microsoft Windows, you need to install it.
Emacs: xah-find.el, Find Replace in Pure Elisp
It does not require grep, is easier to use, and provides other features.