From 781e3a2f4b4c61dc9e9fb4b53dc4c4251f0cd44c Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 26 Jun 2025 16:18:22 +0200 Subject: [PATCH] vim:keys: Substitute only inside visual selection When substituting in a visual block selection, the automatically inserted range `'<,'>` is not enough as the pattern will also match outside of the selection on the same lines. To only match inside the selection, insert the `\%V` atom. Theoretically the pattern will also need the atom before the last character to make sure that everything is inside the selection, but this can be inserted manually when needed (see `:h /\%V`). --- .config/vim/vimrc.d/40-keys.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.config/vim/vimrc.d/40-keys.vim b/.config/vim/vimrc.d/40-keys.vim index bd8490b..4737572 100644 --- a/.config/vim/vimrc.d/40-keys.vim +++ b/.config/vim/vimrc.d/40-keys.vim @@ -27,13 +27,13 @@ nnoremap ! T " Substitute command if (exists('+inccommand') && &inccommand != '') nnoremap S :%s/ - vnoremap S :s/ + vnoremap S :s/\%V else " This does not work with live previewing commands (inccommand) since the " replace pattern is already defined and thus everything matching the search " pattern is just deleted. nnoremap S :%s//gc - vnoremap S :s//gc + vnoremap S :s/\%V/gc endif " Interact with the system clipboard