Preview Mass Text Replacements with Emacs 30.1 replace-regexp-as-diff
Using regular expressions to perform text replacements can be tricky. Without robust undo support or version control, a preview of the changes, or checking every replacement before it’s being made, you can end up with false-positive matches and mess things up.
Now Emacs 30.1 released and ships with yet another way to perform mass text replacements more safely: Use run-of-the-mill diffs to show the changes that would be made if you applied a regular expression-based replacement. I love the ingenuity of this idea, because it is so obvious.

It’s usually rather cumbersome to use the output of diff
for anything but sharing patches.
You probably don’t interact with the difference of 2 files directly in your day-to-day life.
Tooling for the job, consequently, is ‘meh’.
Unless you live in Emacs, where viewing a diff is just a step removed from applying changes hunk-by-hunk.
Emacs 30.1 comes with 3 new interactive functions:
replace-regexp-as-diff
for replacements in a single buffer,
multi-replace-regexp-as-diff
for multiple buffers,
and dired-do-replace-regexp-as-diff
to perform replace-regexp-as-diff
on marked files in the directory editor.
While we have project-query-replace-regexp
,
the interactive cousin that asks for each and every replacement whether you want to perform the change,
note that there’s no project-replace-regexp-as-diff
.
So you can’t run a search on all files in a project just yet.
Most of the time, I’ll still be reaching for grep
(or rather rg
) and wgrep
to edit search result buffers because I like the user interface so much.
You can search via regular expressions in multiple files in a project and then interactively edit the search results.
That’s an interactive live preview of the changes that you want to make.
Similar to the diff preview, it’s like a sandbox:
you don’t actually change anything before you apply the changes.