Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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:

 01.png
 02.png
 03.png
 04.png

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
add comment

Your Answer

 
discard

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.