How do I do a Find and Replace within a selection in vi
?
6 Answers
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)

- 73,098
- 23
- 151
- 149
-
64Caveat: the range only works linewise! If your selection contains only part of a line the search and replace will work on the whole line. Getting partial line find and replace requires scripting afaik. – user55400 Apr 23 '09 at 08:01
-
3I suggest temporarily moving it to a different line, doing the replace, and moving it back. – Benjamin Atkin May 02 '14 at 22:59
-
what does s in `s/...` stand for? – theprogrammer Jun 30 '21 at 20:33
-
1@theprogrammer s stands for substitute. See [this](https://www.thegeekstuff.com/2009/04/vi-vim-editor-search-and-replace-examples/) for some samples. Use the command `vimtutor` to read the manual (it is very friendly to read). – Cyriac Antony Nov 19 '22 at 05:55
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.

- 66,836
- 64
- 257
- 336
-
2Should this work for a replacement when visually selecting part of a line? I can't make it work for replacing spaces with return. I'm selecting part of the line and they using `:%s/\%V /\r/g` with no joy. The first space is replaced, but the following 4 remain untouched. – bentayloruk Jan 20 '15 at 21:59
-
I just tried it, and it works for all cases I tried, except for replacing with a return ;-) I think it must be because the first replace breaks the selection into a new line, which is no longer in the selection. As a workaround you could replace with a unique string, e.g. EZ_REPLACE, then do a global search and replace for that... – Brad Parks Jan 21 '15 at 02:17
-
doesn't play well when recoded in a macro. replaying the macro over a range of lines doesn't work. – Peter Perháč Oct 21 '16 at 20:25
-
2For more info, see https://vim.fandom.com/wiki/Search_and_replace_in_a_visual_selection – Cyriac Antony Feb 24 '19 at 04:50
-
-
I just tried it and it seemed to work fine as I originally suggested? – Brad Parks Nov 22 '20 at 01:07
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.

- 332,285
- 67
- 532
- 628
-
2This should theoretically work as intended, but as somebody else pointed out, in practice is replaces anywhere within the lines where the mark `<` starts and where `>` ends. Maybe its possible to make it work with the `\%'>` atom.. but my initial experiments failed. – alpha_989 Jan 25 '18 at 19:31
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.

- 61
- 2
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.

- 49,562
- 17
- 52
- 70