Search and replace within Emacs evil-mode's visual blocks (rectangle selections).
The evil-visual-replace package provides versions of the emacs query-replace
(M-%) and replace-regexp
(C-M-%) commands which work
for evil-mode visual-state, including visual blocks (rectangular regions).
The native emacs versions don't understand evil's visual blocks, and treat them as normal regions.
Note that these commands are specifically intended for visual state and have
barely been tested in non-visual states. Rather than globally replacing
the native commands, it is recommended to rebind them in evil-visual-state-map
.
Install from melpa or by placing evil-visual-replace.el
on your load-path
and
executing:
(require 'evil-visual-replace)
To replace the M-% and C-M-% bindings in
evil-visual-state-map
, simply call:
(evil-visual-replace-visual-bindings)
If you prefer to give the commands their own bindings, you can bind them like this:
(define-key evil-visual-state-map (kbd "C-x M-%") 'evil-visual-replace-query-replace)
(define-key evil-visual-state-map (kbd "C-x C-M-%") 'evil-visual-replace-replace-regexp)
Or to bind them with evil-leader (eg. on <leader>
M-% and
<leader>
C-M-%):
(evil-leader/set-key
"M-%" 'evil-visual-replace-query-replace
"M-C-%" 'evil-visual-replace-replace-regexp )
If pcre2el is installed, the command
evil-visual-replace-pcre-replace-regexp
can be used instead of
evil-visual-replace-replace-regexp
. Bind it as above, for example:
(define-key evil-visual-state-map (kbd "C-x C-M-%") 'evil-visual-replace-pcre-replace-regexp)
Alternatively, pass a non-nil argument to evil-visual-replace-visual-bindings
:
(evil-visual-replace-visual-bindings t)
For consistency, this will also bind C-M-% to
pcre-query-replace-regexp
for non-visual states.
Due to the line-wise processing of visual-blocks, the behaviour of ! is different in a visual block replace. Rather than replacing all remaining occurrences, it only replaces those on the current line. Similarly, replacements of newline characters will not work in visual blocks.