I want to do substitution in a partial of a line.
But when I type :'<,'>s/xx/yy/g, it always do it in the whole line.
How can I tell vim to do it in the exact selection area??


1 Answer 1


Add \%V to your search. From :h /\%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,
    e.g.: >
<   Only works for the current buffer.

So you want to do :'<,'>s/\%Vxx/yy/g

  • actually you want more something like :s/\%Vx\%Vx/yy/g Nov 23, 2016 at 7:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.