Interactive Find and Replace String Patterns on Multiple Files

X

Xah Lee

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:
http://xahlee.org/emacs/find_replace_inter.html

Xah
(e-mail address removed)
∑ http://xahlee.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top