Use ‘M-x find-grep-dired’ interactively to search and replace a regex across all files in a specified directory and subdirectories which match a pattern.

Basic usage is:

  1. invoke, passing your search phrase M-x find-grep-dired RET catch (FooException RET
  2. mark buffers of interest % m .php$ RET
  3. invoke search and replace across the marked buffers Q catch (FooException RET catch (BarException RET

At this point, Emacs will cycle through every match in every file and ask you to confirm the replacement. If you press !, it will replace the rest of the matches in the current file, and start prompting you for the next; Y will replace every match in every file with no further prompting. `M-,’ restarts the interactive prompting if you break out of it.

Going back to the find-grep-dired buffer, and C-x s will save all changed files.

See DiredSearchAndReplace for more information.

See also DiredFindInLisp for ‘M-x find-grep-dired-lisp’.

CategoryDirectories CategorySearchAndReplace