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`).
This commit is contained in:
2025-06-26 16:18:22 +02:00
parent e9bb145fe1
commit 781e3a2f4b

View File

@@ -27,13 +27,13 @@ nnoremap <C-w>! <C-w>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<Left><Left><Left>
vnoremap S :s//gc<Left><Left><Left>
vnoremap S :s/\%V/gc<Left><Left><Left>
endif
" Interact with the system clipboard