2

When I type the pattern part of a global command (like so: :global/xxx), the windows in front of me get highlighted as if I was typing directly into a / or ? style search command.

I'm writing a small wrapper around :lgrep that I wish to have the same incremental highlighting functionality. Can I register my function as a specific type such that when I'm typing in the first bit of it (like so: :call my_function(xxx) it incrementally highlights?

1 Answer 1

2

You can use the CmdlineChanged event to set the highlight:

command -nargs=+ Test echo <q-args>

augroup hl-Test
    au!
    au CmdlineChanged *
                \  if getcmdtype() == ':' && getcmdline()[: 3] == 'Test'
                \|   let @/ = getcmdline()[4 :]->trim()
                \|   setl hlsearch
                \|   redraw
                \| endif
augroup end

The getcmdtype() check ensures it only applies for the : commandline (and not e.g. / for search).

The @/ register contains the current search command, which can be set manually; :setl hlsearch highlights the contents of @/, and you need to do an explicit redraw to make it actually highlight.

One thing this doesn't do is scroll to the first instance if it's outside of the current screen; doing that is quite a bit more involved: you need to get the contents of the current view, check if anything matches, if not scroll the view to the first match, and then also restore it after something is typed that no longer matches anything in the view.

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.