472,146 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Interactive Find and Replace String Patterns on Multiple Files

Interactive Find and Replace String Patterns on Multiple Files

Xah Lee, 2006-06

Suppose you need to do find and replace of a string pattern, for all
files in a directory. However, you do not want to replace all of them.
You need to look at it in a case-by-case basis. What can you do?

Answer: emacs.

Here's how you do it.
Select Target Files

Start emacs by typing “emacs” in the command line interfaceprompt.

Now you need to locate the directory and files you want to
find/replace. Type “esc x find-dired”. (then press Enter) Then,
give a directory name, e.g. “/Users/mary/myfiles”

Emacs will ask you with the prompt “Run find (with args): ”.. If you
need to do the replacement on all html files, then give “-name
"*html"”. If you don't care about what kind of file but simply all
files under that dir, then give “-type f”.

Now, you will be shown the list of files, and now you need to
“mark” the files you want the regex find-replace to work on.. You
mark a file by moving the cursor to the file you want, then press m.
Unmark it by pressing u. To mark all files by a regex, type “% m”,
then give your pattern. Suppose you want to mark all html files, then
type “% m html$”.
Interactive Find & Replace

Now, you are ready to do the interactive find replace. For simplicity,
let's say you just want to replace the word “quick” by “super”
depending on the context. Now, type “esc x
dired-do-query-replace-regexp”. It will prompt you for the regex
string and the replacement string. Type “quick” then “super”.

Now, emacs will use your pattern and check the files, and stop and show
you whenever a match occurred. When this happens, emacs will prompt
you, and you have a choice of making the change or skip the change. To
make the change, type y. To skip, type n. If you simply want emacs to
go ahead and make all such changes to the current files, type “!”.
If you want to cancel the whole operation, type control-g.
Saving the Changed Files

Now, after you went through the above ordeal. There is one more step
you need to do, and that is saving the changed files. This you can do,
by typing “esc x list-buffer”, then move the cursor to the file you
want to save and press s. It will mark the file for later save action.
Type u to unmark. Once you are done, type x to execute the saving of
all S marked files. (in emacs, opened file is called “buffer”.
Disregard ohter things there.)

Alternatively, you can also type “esc x save-some-buffers”.Then
emacs will show each buffer for you and ask if you want it saved.

If you have emacs version 22, you can use “M-x ibuffer” to mark all
files you want to save by a regex.


PS if anyone know any tool or perl/python/lisp program that can also do
this, i'd be interested to know. Thanks.

This post is archived at:


Jun 14 '06 #1
0 2928

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by hokieghal99 | last post: by
5 posts views Thread by djc | last post: by
3 posts views Thread by mouac01 | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.