R‎ > ‎Emacs Commands‎ > ‎

Find and Replace Across Multiple Files in Emacs

Call “dired” or “find-dired” to list files in dir and all subdirs.
  • Mark the files you want. You can mark by regex by typing “%m”
  • Type “Q” to call dired-do-query-replace-regexp.
  • Type your find regex and replace string.
  • For each occurance, type “y” to replace, “n” to skip, “!” for replacing all occurances in current file. Type “Ctrl+g” to abort the whole operation.
  • To do the replacement on all files without further asking, type “Y”. To skip all possible replacement on the rest of the current file, type “N”. (These are Emacs 23 only)
  • Call “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.
  • Step-by-Step Guide

Select Target Files
Start emacs by typing “emacs” in the command line interface prompt. (Or, double click the Emacs icon if you are in a Graphics User Interface environment)

Selecting Files In A Directory
First you need to select the files you want to do the replace. Use the graphical menu “File‣Open Directory”. Emacs will ask you for a directory path. Type the directory path, then press Enter.

Now, you will be shown the list of files, and now you need to “mark” the files you want the regex find-replace to work on. You mark a file by moving the cursor to the file you want, then press “m”. Unmark it by pressing “u”. (To list subdirectories, move your cursor to the directory and press “i”. The subdirectory's content will be listed at the bottom.) To mark all files by a regex, type “%m”, then type your regex pattern. For example, if you want to mark all html files, then type “%mhtml$”. (You can find a list of the mark commands in the graphical menu “Mark” (this menu appears when you are in the dired mode).)

Selecting Files In A Directory And All Its Subdirectories
If you want to do find and replace on files inside a directory, including hundreds of subdirectories, here's a method to select all these files.

Press the “Esc” key, then press the x key, then type “find-dired”. (then press Enter) Then, type a directory name, e.g. “/Users/mary/myfiles”

Emacs will ask you with the prompt “Run find (with args): ”. If you need to do the replacement on all html files, then type “-name "*html"”. If you don't care about what kind of file but simply all files under that dir, then give “-type f”.

Now, mark the files as described above.

Interactive Find & Replace
Now, you are ready to do the interactive find replace. For simplicity, let's say you just want to replace the word “quick” by “super”. Now, type “Esc x dired-do-query-replace-regexp”. It will prompt you for the regex string and the replacement string. Type “quick”, enter, then “super”.

Now, emacs will use your pattern and check the files, and stop and show you whenever a match occurred. When this happens, emacs will prompt you, and you have a choice of making the change or skip the change. To make the change, type “y”. To skip, type “n”. If you simply want emacs to go ahead and make all such changes to the current file, type “!”.

If you want to cancel the whole operation without saving any changes you've made, type “Ctrl+g”, then exit emacs using the menu “File‣Exit Emacs”.

Saving the Changed Files

Now, after you went through the above ordeal. There is one more step you need to do, and that is saving the changed files.

If you have emacs version 22, you can type “Esc x ibuffer” to go into a buffer listing mode, then type “*u” to mark all unsaved files, then type “S” to save them all. (that's shift-s)

If you are using a emacs version 21, then you can do this: type “Esc x list-buffer”, then move the cursor to the file you want to save and type “s”. It will mark the file for later save action. Type “u” to unmark. Once you are done, type “x” to execute the saving of all files marked for save. (in emacs, opened file is called “buffer”. Disregard other things there.)

Alternative to the above options, you can also type “Esc x save-some-buffers”. Then emacs will display each unsaved file and ask if you want it saved.