Find and Replace within selection in `vi`

39,326

Solution 1

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

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

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

Some more help here Search and replace in a visual selection

Solution 5

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.

Share:
39,326

Related videos on Youtube

Agnel Kurian
Author by

Agnel Kurian

Software Engineer with 18+ years of experience developing software in a wide range of areas: desktop, web, user interfaces, 2D/3D graphics, geometry, encryption and even structural analysis! I have a degree in Civil Engineering and a diploma from NIIT. I'm very comfortable on Linux. I like solving interesting problems.

Updated on November 28, 2020

Comments

  • Agnel Kurian
    Agnel Kurian over 3 years

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

  • Lazarus
    Lazarus about 15 years
    The local is local to the current line
  • Gorm Casper
    Gorm Casper about 10 years
    I suggest temporarily moving it to a different line, doing the replace, and moving it back.
  • bentayloruk
    bentayloruk over 9 years
    Should 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.
  • Brad Parks
    Brad Parks over 9 years
    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...
  • Peter Perháč
    Peter Perháč over 7 years
    doesn't play well when recoded in a macro. replaying the macro over a range of lines doesn't work.
  • alpha_989
    alpha_989 over 6 years
    This 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.
  • Cyriac Antony
    Cyriac Antony about 5 years
  • Atralb
    Atralb over 3 years
    @BradParks AFAIK, you need \%VSEARCH%V instead of only \%VSEARCH
  • Brad Parks
    Brad Parks over 3 years
    I just tried it and it seemed to work fine as I originally suggested?
  • theprogrammer
    theprogrammer almost 3 years
    what does s in s/... stand for?