Wimm-select Vim and regex

I had a problem with visually selecting and replacing a regex. When I select some text that does not contain the whole line and press: to bring the command line up and do something like

:s/T/t/ 

Then the first match of the string is performed (regardless of whether it is selected or not). So for example, I have text

 Test Text here 

and I visually select the word Text , then run the above replacement, in the end we get

 Test Text here 

what I do not want.

Any ideas on how to achieve the right result?

Edit: actual command line

 '<,'>s/T/t/ 

default is vim when you click: with visual selection.

+6
source share
1 answer

You can use \% V (see http://vimdoc.sourceforge.net/htmldoc/pattern.html#//%V )

 \%V Match inside the Visual area. When Visual mode has already been stopped match in the area that |gv| would reselect. This is a |/zero-width| match. To make sure the whole pattern is inside the Visual area put it at the start and end of the pattern, eg: /\%Vfoo.*bar\%V Only works for the current buffer. 

So:

 :s/\%VT/t/ 

If you want to replace multiple hits, add / g

 :s/\%VT/t/g 
+10
source

Source: https://habr.com/ru/post/904080/


All Articles