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 · 4 comments
Assignees
Labels

Comments

@purpleP
Copy link

@purpleP 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

This comment has been minimized.

Copy link
Owner

@romainl 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

This comment has been minimized.

Copy link
Owner

@romainl 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
@romainl romainl added the enhancement label Dec 24, 2016
@purpleP

This comment has been minimized.

Copy link
Author

@purpleP purpleP commented Dec 25, 2016

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

@romainl

This comment has been minimized.

Copy link
Owner

@romainl romainl commented Dec 26, 2016

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

@romainl romainl closed this Dec 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.