3

I'm trying to implement a search function that finds a pattern and jumps to it without overriding the current searched for & highlighted pattern.

Specifically, I want to be able to type out the pattern and jump to it just like how / and ? are implemented in neovim; where when I start typing it immediately starts highlighting & jumping in real time. The only difference is that after jumping, I want to keep the previous highlighted/searched for pattern.

I took a look at several pages including this, but the problem is that these solutions only seem to work with pre-specified patterns, whereas in my case I want to type out the pattern via / or ?.

One solution may be to write a function that stores current pattern, calls //? and after jumping replaces the pattern.

function! ForwardJumpToPattern()                                                                                                                                                              
    let search_original_content=@/                                                                                                                                                          
    normal! /  
    let @/=search_original_content                                                                                                                                                                    
endfunction 

But the problem is that I can't figure out how to call / or ? from inside a function.

6
  • 2
    why not use the search() function? Nov 21, 2022 at 7:20
  • I tried that, but it is not interactive like searching via / or ?
    – WalksB
    Nov 21, 2022 at 8:01
  • 1
    Turns out it is interactive in neovim, not vim. I edited the Q
    – WalksB
    Nov 21, 2022 at 17:04
  • so you want the 'incsearch' option setting, not interactive. But I don't understand, I thought you already have the search term? If you know the pattern and the searchterm, you can use matchadd() to highlight the pattern Nov 22, 2022 at 13:03
  • @christian I dont have the search term already, otherwise the search function would work. And yes! Thanks for correcting the terminology. Can I call incsearch from within a function?
    – WalksB
    Nov 23, 2022 at 17:57

1 Answer 1

0

You can use the search() function.

Call:

  • search(pattern) to simulate /pattern
  • search(pattern, 'b') to simulate ?pattern
4
  • It's not interactive like when i search via / or ?. Perhaps that is because I'm using the latest neovim and not regular vim?
    – WalksB
    Nov 21, 2022 at 8:03
  • I'm not sure to understand what you mean by interactive. Could you elaborate? Nov 21, 2022 at 8:09
  • I just realized that in vim it is not interactive. I'm using neovim. When I begin typing out characters after / in neovim, it immediately starts to highlight the results (before I hit enter), whereas in vim it only highlights and jumps to the results after carriage return.
    – WalksB
    Nov 21, 2022 at 16:36
  • 1
    I edited the question to specify this
    – WalksB
    Nov 21, 2022 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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