Emacs Redux: Preview Regex Replacements as Diffs

Wait 5 sec.

If you’ve ever hesitated before running query-replace-regexp across a largefile (or worse, across many files), you’re not alone. Even experienced Emacsusers get a bit nervous about large-scale regex replacements. What if the regexmatches something unexpected? What if the replacement is subtly wrong?Emacs 30 has a brilliant answer to this anxiety: replace-regexp-as-diff.How it worksRun M-x replace-regexp-as-diff, enter your search regexp and replacementstring, and instead of immediately applying changes, Emacs shows you a diffbuffer with all proposed replacements. You can review every single change infamiliar unified diff format before committing to anything.If you’re happy with the changes, you can apply them as a patch. If somethinglooks off, just close the diff buffer and nothing has changed.Multi-file supportIt gets even better. There are two companion commands for working across files: multi-file-replace-regexp-as-diff — prompts you for a list of files andshows all replacements across them as a single diff. dired-do-replace-regexp-as-diff — works on marked files in Dired. Mark thefiles you want to transform, run the command, and review the combined diff.The Dired integration is particularly nice — mark files with m, run the commandfrom the Dired buffer, and you get a comprehensive preview of all changes.Note to self - explore how to hook this into Projectile.A practical exampleSay you want to rename a function across your project. In Dired: Mark all relevant files with m (or % m to mark by regexp) Run dired-do-replace-regexp-as-diff Enter the search pattern: \bold_function_name\b Enter the replacement: new_function_name Review the diff, apply if it looks goodNo more sweaty palms during large refactorings.1Closing ThoughtsI have a feeling that in the age of LLMs probably few people will getexcited about doing changes via patches, but it’s a pretty coolworkflow overall. I love reviewing changes as diffs and I’lltry to incorporate some of the commands mentioned in this articlein my Emacs workflow.That’s all I have for you today. Keep hacking! Assuming you’re still doing any large-scale refactorings “old-school”, that is. And that you actually read the diffs carefully! ↩