How to search and replace in vertical selection in vim?
I know I can do search and replace on a horizontal selection with:
:'<,'>s/search/replace/g
But I could not figure out how to do this with vertical selection. I tried the same method above, only having a vertical selection, but apparently vim will search and replace on the entire line, not just the selection.
Searched the internet to no avail. Thank.
+3
source to share
1 answer
You can use \%V
in your regex, it will match the position inside the visual area:
\%V Match inside the Visual area. This is a `/zero-width` match.
If you render the block:
search search search search
searc+---------------+earch
searc| search search |earch
searc| search search |earch
searc+---------------+earch
search search search search
After running this command:
:'<,'>s/\%Vsearch\%V/replace/g
It will become as follows:
search search search search
searc+---------------+earch
searc| replace replace |earch
searc| replace replace |earch
searc+---------------+earch
search search search search
+8
source to share