In Vim, how to search and replace in visual selection?

Search and replace in the whole text in Vim can be done by :%s/from/to/gc. But how to search and replace in visual selection only?

asked Jan 22, 2016 by anonymous

1 Answer

The \%V atom in Vim restricts a pattern so that it matches only inside the visual selection.

So, your command for searching and replacing only in the visual selection can be

:%s/\%Vfrom/to/gc

In addition, you can use marks for the beginning and end of the visual selection as the start,end range for the :s command:

'<  start line
`<  start character
'>  end line
`>  end character
answered Mar 15, 2016 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...