53

I know that with Vim's substitution command you can specific a range of lines:

:12,24s/search/replace

I want to be able to specify a range with normal searches as well. Something like

:12,24/search

Since that doesn't seem to work (at least on my Vim configuration), does anybody know how to achieve this?

7 Answers 7

50

Great answer from akira. But after some digging, I found an alternative. It's not as elegant but easier to type in:

 :12,24g/search/

This will give you one annoying prompt but it will end up on the first line within the range containing the sought string.

2
  • This worked for me without any prompt annoyance. So the command << 195,208g/print/s/$/ # debug/ >> appended ' # debug' to the lines containing 'print' from 195 to 208, and only those lines. Maybe a version issue? I'm using VIM 7.2.330.
    – chernevik
    Apr 28, 2013 at 11:58
  • 7.3 here, and it says Press ENTER or type command to continue. I'm not well enough versed in vim to tell where the differences might come from. Apr 29, 2013 at 20:52
38
:help search-range

and then

:help /\%>l

so essentially:

/\%>12l\%<24lsearch
5
  • I don't see what this adds to the :start,stop/pattern/s/tgt/repl/ pattern offered by Carl.
    – chernevik
    Apr 28, 2013 at 11:59
  • 7
    @chernevik: check the timestamp of the answers.
    – akira
    Apr 28, 2013 at 12:07
  • ^ For the 'correct way'. Vim can give many ways to accomplish a task.
    – 0xc0de
    Jul 28, 2016 at 7:47
  • Although, this is absolutely correct and accepted answer, the answer by Habi (stackoverflow.com/a/3264324/633910) works practically better for me, most times, with the visual range selection.
    – bbv
    May 12, 2020 at 9:02
  • It's worth noting that there are advantages to using the regular search command, namely that you have access to the full set of match navigation commands. For me, searching without (permanently) moving my cursor (i.e., <C-g>/<G-t> to iterate through the matches) is really handy when I want to quickly check something.
    – circld
    Feb 4 at 20:05
27

Do you really need line numbers? Another way could be to select the range visually.

  1. select the range using v, V or whatever
  2. press ESC to unselect the range
  3. search using /\%Vwhat_to_search to search for 'what_to_search' in the previously selected range.

This is lesser to type, but not directly what you have asked for ;-)

See :help %V

[EDIT] Great, I have just learned that the range to search in can be changed after doing the search by selecting another range, unselecting this range again by pressing ESC and pressing n to repeat search. Vim is really always good for pleasant surprises.

21

Keep using the substitution command, but append the gc flags to your original example.

:12,24s/search//gc

From :help search-range

[To search within a range] use the ":substitute" command with the 'c' flag.

Example: :.,300s/Pattern//gc

This command will search from the cursor position until line 300 for "Pattern". At the match, you can type 'q' to stop, or 'n' to find the next match.

5

If there marks say a and b, then the search can be restricted to the region between a and b using

/\%>'a\%<'bSearchText

This can be simplified with a cmap

cmap vmsab /\%>'a\%<'b
cmap vmscd /\%>'c\%<'d
2

If you would like to search to the end of the file, use $:

:3,$s/pattern//gn

This will search from 3-d line to the end

1

Using Narrow Region plugin we can open a temporary buffer with the range we need to search or change

:900,1000NarrowRegion

Then we can meke a search

/thing

Or a change and write the buffer back

:%s/this/that/g
:wq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.