Multi-file Search and Replace in Emacs

Menu

Emacs can perform multi-file search and replace in a variety of ways.

This is a simple way to do it:

  1. Get a Dired-like listing of the files to perform the search on, using one of these commands:
    • find-name-dired, which searches for all the files matching a pattern from a given dir
    • find-grep-dired, which searches for a regular expression in all files from a given dir
  2. Mark all the files using either one of these Dired commands:
    • % m and then a regular expression matching the name of the files to perform the search on
    • % g and then the string/regular expression being searched
  3. Use dired-do-query-replace-regexp to perform the actual search and replace

Thus, for instance:

M-x find-name-dired ~/Sources/rails_app .*\.rb
% m .*\.rb
M-x dired-do-query-replace-regexp

The Search Across Files section in the Emacs Wiki lists many other options, similar to the one I described above. If you are looking for an approach more similar to the function provided by IDEs, you might want to have a look at Projectile.