Find and Replace within selection in `vi`

Agnel Kurian picture Agnel Kurian · Apr 21, 2009 · Viewed 31.5k times · Source

How do I do a Find and Replace within a selection in vi?

Answer

Chad Birch picture Chad Birch · Apr 21, 2009

Select the text in visual mode (I assume that's what you're doing), then press : to start typing a command, you'll see something like this appear in the command line:

:'<,'>

That means that the command will apply to the selection. Then type s/search/replace/ and hit enter. (Add a g after the third slash if you want to replace all matches, and a c if you want a confirmation for every replace)