2

I find it obnoxious that highlight search stays on until you manually turn it off. If I have edited some text after searching, 99% of the time that means I found what I was looking for and I don't need highlighting anymore. How can I make it so highlighting turns off when I edit a file? Here's what I tried, but for some reason it doesn't seem to have an effect.

augroup allfiles
    autocmd!
    autocmd TextChanged,TextChangedI * :nohl
augroup END
4

:help noh explains why this isn't working.

  :noh[lsearch]     Stop the highlighting for the 'hlsearch' option.  It
        is automatically turned back on when using a search
        command, or setting the 'hlsearch' option.
        This command doesn't work in an autocommand, because
        the highlighting state is saved and restored when
        executing autocommands |autocmd-searchpat|.
        Same thing for when invoking a user function.

You may be interested in the plugin https://github.com/romainl/vim-cool . The basic trick is to use feedkeys and a <plug> mapping with execute(:

inoremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    endif
    sil call feedkeys("\<Plug>(StopHL)", 'm')
endfu
au InsertEnter * call StopHL()

From https://github.com/romainl/vim-cool/issues/9#issue-196870339 .

  • God that's annoying. This gets in my way way more than it helps me. – Daniel Kaplan Dec 10 '17 at 7:24
  • Actually I'm unclear about something: If I install this plugin, do I need your code? – Daniel Kaplan Dec 10 '17 at 7:41
  • 1
    No- the plugin does what this code does and more. You should not use both together. – Mass Dec 10 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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