On 2023-01-13, Salman Halim wrote:
> Hello,
>
> I want to use something like search(), but only across a specific set of lines.
> The problem is that search() always starts at the current cursor line.
>
> For example, I want to find a match for 'cat', but only in lines 50 to 100. If
> my cursor is before line 50 when I start, I can prefix my expression with \%>49
> and pass 100 as the stopline, but that doesn't work if my cursor is AFTER line
> 50 as it starts the match at the cursor location.
>
> I don't want to first move my cursor. I'm also hesitant to use getline() to
> grab all the lines, do an indexOf type search and then return the line number
> because that's going to get memory intensive if I have a large file.
>
> Does anybody have any thoughts on this? I'd appreciate advice.
I don't know your specific requirements, but not wanting to move
your cursor should not prevent you from using search(). Just save
the cursor position, save the window position, move the cursor to
the start of the search range, and when you're done with the
searching, restore the window position and the cursor position.
I've done this in a number of plugins and I can't even tell that the
cursor ever moved.
Regards,
Gary