Find and Replace within selection in `vi`

VimEditorViReplace

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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)

Solution 2 - Vim

Most of the other solutions suggested here work over the ENTIRE line in which the selection occurs, which may not be what you want.

To search and replace ONLY in the selection, first visually select the text, then use a command like so:

:%s/\%VSEARCH/REPLACE/g

This will do the search and replace only in the visually selected section, replacing SEARCH with REPLACE. If you have more than one line selected, this will work over multiple lines too.

Solution 3 - Vim

If you used Visual Mode to select, then:

:'<,'>s/regex/replacement/options

VIM will place the range ('<,'>) automatically if you go into Command Line Mode (by pressing ':') from within Visual Mode.

Solution 4 - Vim

Solution 5 - Vim

The range of Ex commands are specified line-wise (see *cmdline-ranges*), and when : is pressed while there is a visual selection, the line range is automatically specified on the command line as '<,'> (see *v_:*), which makes the :s[ubstitute] command operate on the whole lines unless the visual selection boundaries are specified in the search pattern with \%V (see */\%V*), e.g. /\%Vvi\%Vm matches "vim" only within the visual selection, where the end of the selection is specified right before the end of the search pattern since each \%V specifies the next character as the start or end of the visual selection, and thus /\%Vvim\%V would require the visual selection to continue after 'm' to match "vim". Note that using the second \%V in a search pattern isn't necessary unless a match is required to be right at the border of or only partly in the visual selection.

Solution 6 - Vim

If you want to do a global search and replace (with optional regexes) for all instances in the file, I would do the following:

:%s/foo/bar/g

Omit the g to do a local replace.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAgnel KurianView Question on Stackoverflow
Solution 1 - VimChad BirchView Answer on Stackoverflow
Solution 2 - VimBrad ParksView Answer on Stackoverflow
Solution 3 - VimTomalakView Answer on Stackoverflow
Solution 4 - VimLazarusView Answer on Stackoverflow
Solution 5 - VimdeticView Answer on Stackoverflow
Solution 6 - VimlfaraoneView Answer on Stackoverflow