VIM: Search only between specific line numbers?

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.


:help search-range

and then

:help /\%>l

so essentially:

/\%>12l\%<24lsearch

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.

Tags:

Vim

Search