Using the wgrep package in Emacs to rewrite grep results from lots of files
| 11ty, emacsacdw let me know that my Emacs News links to https://reddit.com didn't work for him in Firefox on Linux, but that changing the links to https://www.reddit.com would make them work. I was glad that I had migrated my blog from Wordpress to the 11ty static site generator. All my blog posts were HTML files, and I could just whiz through them all.
In the past, I might have used find-dired
or find-grep-dired
to
get a list of files. Then I would record a keyboard macro to open a
file, search and replace the text, save the buffer, and then kill the
buffer, and I would just repeat this macro until an error occurred.
Some time ago, I learned about using Q
(dired-do-find-regexp-and-replace
) in Dired to replace regular
expressions in marked files, using Y
to accept all the replacements.
That would then let me use C-x s
(save-some-buffers
) to save all
the modified buffers with !
. The bufler package is handy for closing
a whole bunch of buffers of a certain type, although ibuffer or helm
can handle that too.
Emacs being Emacs, there's an even better way to do it. I used M-x
grep
to find the lines to change, the wgrep package to make the
results editable, and M-x replace-string
to change things. A quick
C-c C-e
(wgrep-finish-edit
) later, and I was able to change 5485
matching lines in 302 files.
It's so nice being able to deal with things in plain text. Yay for Emacs! There's always something more to learn.