Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a solution to how to perform a search and replace in all files in a directory using Emacs.

share|improve this question
up vote 7 down vote accepted

I've found the solution myself. Dired has builtin functionality to do that. See article Interactively Find and Replace String Patterns on Multiple Files for further details.

share|improve this answer

As mentioned in the other answer you'll want to use dired.

First mark the files via m key. Alternatively you can use % m to dired-mark-files-regexp (in the case of marking all files, put .* as your regexp).

You'll want to use the % R to dired-do-rename-regexp. This will limit the rename regular expression to the marked files. You will have to use Emacs regexp syntax which differs from regular unix syntax.

If I had a number of files in a directory named the following:


And I wanted them renamed so they all were prefixed with chapter-01, I would input the following keystrokes.

% m - mark regexp
.*\.png - mark all png files
% R - rename regexp
\([0-9]+\)\(\.png\) - matching regexp, save the parens into variables
chapter-01-\1\2 - utilizing the previous variables`
! - during the query to denote rename all the rest
share|improve this answer
Thanks for your answer, but it's rather describing how to mass rename files than how to replace string in their content. – Török Gábor Feb 19 '10 at 8:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.