Friday, January 13, 2023

Re: Searching across a range of lines

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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20230113221402.GF6446%40phoenix.

No comments: