Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn off highlight on CursorMoved and InsertEnter without remapping (doesn't break search operator pending mode) #9

Closed
purpleP opened this issue Dec 21, 2016 · 6 comments
Assignees

Comments

@purpleP
Copy link

purpleP commented Dec 21, 2016

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).

Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.

So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end
@romainl
Copy link
Owner

romainl commented Dec 24, 2016

This works amazingly well and is a lot cleaner than what I did. Can I use it as starting point for a new branch?

romainl pushed a commit that referenced this issue Dec 24, 2016
@romainl
Copy link
Owner

romainl commented Dec 24, 2016

Please take a look at branch "purplep": https://github.com/romainl/vim-cool/tree/purplep.

@romainl romainl self-assigned this Dec 24, 2016
@purpleP
Copy link
Author

purpleP commented Dec 25, 2016

@romainl Yes, of course you can. I've leared about OptionSet event from you code. Cool.

@romainl
Copy link
Owner

romainl commented Dec 26, 2016

I merged the "purplep" branch into "master". Thanks a lot!

@romainl romainl closed this as completed Dec 26, 2016
@ghost
Copy link

ghost commented Jul 15, 2021

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).

Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.

So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end

nice.. thanks a lot @purpleP

@purpleP
Copy link
Author

purpleP commented Jul 21, 2021

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).
Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.
So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end

nice.. thanks a lot @purpleP

🙏

Chromosore added a commit to Chromosore/vim-chimera that referenced this issue Jun 29, 2022
This is a left over from the base of this plugin that I borrowed from
@purpleP, see [romainl/vim-cool#9].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants