Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Search only between specific line numbers?

Tags:

vim

search

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?

Thank you.

like image 830
Thomas Avatar asked Jul 16 '10 10:07

Thomas


People also ask

How do I go to a specific line number in Vim?

The Vim goto line number command One can use the G letter. For example, press [ESC] key and type 10G (Shift-g) goto line number 10.

How do I go to line numbers in Linux?

To do this, press Esc , type the line number, and then press Shift-g . If you press Esc and then Shift-g without specifying a line number, it will take you to the last line in the file. To look for the next occurrence after the first, either press n or press / again and then press Enter .

How do I search for something in Vim?

One can search forward in vim/vi by pressing / and then typing your search pattern/word. To search backward in vi/vim by pressing ? and then typing your search pattern/word. Once word found in vim, you can press the n key to go directly to the next occurrence of the word in backwards.


2 Answers

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.

like image 66
Carl Smotricz Avatar answered Sep 20 '22 18:09

Carl Smotricz


:help search-range 

and then

:help /\%>l 

so essentially:

/\%>12l\%<24lsearch 
like image 41
akira Avatar answered Sep 20 '22 18:09

akira