Prettier text replacement with anzu

We recently introduced basic text replacement, but it is easy to make things a bit snazzier. You can preview the replacement by installing the anzu package, and then adding the following to your emacs config file:

(require 'anzu)
(global-anzu-mode)
(global-set-key (kbd "M-%") 'anzu-query-replace)
(global-set-key (kbd "C-M-%") 'anzu-query-replace-regexp)

This is illustrated in the example below from my post on renaming multiple files in dired – note how when I replace “foo” with “bar” you see the replacement text previewed next to the original text

dired-rename.gif

If you use my recommended setup, prelude, this behaviour is the default,so you shouldn’t need to do anything.

  • tprk77

    Nice tip. I will definitely start using this. (Makes me think I should try Prelude, too. Just to see what other extensions I might have missed.)

  • Would be greatly improved if it was a non-query version – i.e. just performed the previewed replacement, rather than asking you. Easily 95% of my replacements are unconditional across a selected region.

    (For your dired case, I typically use multiple cursors or iedit.)