I am terrible at remembering regular expression syntax. When replacing in by buffer with a regexp, I would like Emacs to highlight the possible matches while I am typing the regexp, so that I can see on the fly whether I am getting what I want. Is there any way of achieving this?
1 Answer
EDIT
I just noticed that my answer is valid for Emacs 29.0.50, but not for Emacs 28.1. Unfortunately, I can not easily find when this was added.
END EDIT
This is default behavior when using M-x replace-regexp
.
Emacs does not show you the result when typing the new pattern (for replacement). However, when using vim style substitution via the [evil]package, then you will also see a preview of the result of the replacement. You can also use it without using evil-mode, by installing and loading evil and then do M-x evil-ex
(which you could bind to your preferred key).
-
It's nice that there is something that works on newer versions, but I am stuck with 26... Is it possible to use the evil-mode command without enabling the vim bindings? Oct 23, 2022 at 13:26
-
Yes, for sure that's possible. Just install and load/require evil (without enabling evil-mode), subsequently you can use
M-x evil-ex
(I've also updated the answer). Of course, you can bind that command to your preferred keybinding Oct 23, 2022 at 19:50