4

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

2 Answers 2

7

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.

0

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
1
  • Thanks for your answer, but it's rather describing how to mass rename files than how to replace string in their content.
    – viam0Zah
    Feb 19, 2010 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .