Search code examples
vimeditorvireplace

Find and Replace within selection in `vi`


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


Solution

  • If your selection is a across one or more lines, this can be done by selecting 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)