4

Search highlight is very convenient for me when I'm searching, but after I'm done with doing stuff with search matches it's distracting to me, so I'm using :noh mapped to F3 everytime I've done what I needed to do with matches.

I'd like to automate this process as it is very annoying to me (this is what vim is about right?). Is it possible to turn off highlighting if n or N isn't pressed for a couple of seconds?

Edit1

Guys, don't bother explain to me how YOU use vim (toggle highlight in insert mode etc). I know exactly what I want and asked precise question. I know you're trying to be helpful, but you're not, sorry.

  • Knowing exactly what you want does not prevent you of maybe learning a new things with different solution. After all you don't know what you don't know... Timing in Vim is a sensible subject, and I think it's better to have a proper action that disable the highlight, hence the proposed solutions. But if you still want to have a timer, have a look at :h timer_start(). – nobe4 Jun 2 '16 at 16:30
  • @nobe4 I agree with you in the general case, but in this specific very simple case there's nothing to learn. Especially after reading this stackoverflow.com/questions/22798473/… – user1685095 Jun 2 '16 at 16:35
  • Well, it seems that there is already a solution here, what do you want more? – nobe4 Jun 2 '16 at 16:37
  • @nobe4 I don't see any working solution there. – user1685095 Jun 2 '16 at 19:22
  • So maybe you should explain what is the difference between the proposed solutions and what you need. And if a solution may not work for you, maybe try to explain what does not work instead of just saying "This isn't working for me." This will be useful for everybody. – nobe4 Jun 3 '16 at 8:04
7

I recently wrote a small plugin that seems to do exactly what you want.

It works by remapping search-related normal mode commands /?nN*#g*g# and <CR> in command-line mode, and installing a cursorMoved autocommand.

  • Okay, I've tried your plugin and it tries to do what I want but fails. It fails on two things basically: disables highlight when leaving insert mode (it shouldn't do that, it should disable it if next n (by n I mean some natural number) isn't search related (like not n, N, or gn) or disable after some time. Next thing it enables hightlight of previous search when typing /. Again it shouldn't do that. It makes no sense at least to me. I don't want to see previous matches when starting new search. It's inconsistent with behaviour of manual :noh – user1685095 Jun 2 '16 at 19:28
4

I actually like to keep the search highlighting, but find it annoying in insert mode. This is something I use in my config to toggle it off when editing:

" Removes the search highlight on insert mode and restores it in normal mode
function! s:toggle_highlight()
  if exists('b:last_hlsearch')
    let &l:hlsearch = b:last_hlsearch
    unlet b:last_hlsearch
  elseif &l:hlsearch
    let b:last_hlsearch = &l:hlsearch
    setlocal nohlsearch
  endif
endfunction

augroup vimrc_search
  autocmd!
  autocmd InsertEnter * call s:toggle_highlight()
  autocmd InsertLeave * call s:toggle_highlight()
augroup END

I also use this key map so I don't have to move my hands from the home row:

nnoremap <leader><space> :nohlsearch<cr>

With faster access to :nohlsearch, I serve as a much better judge for determining when search highlighting is not useful.

3

If your vim has the +timers functionality you can use them like so:

let s:current_timer = -1

function! DisableHL(timerId)
  set nohlsearch
endfunction

function! ResetTimer()
  if s:current_timer > -1
    call timer_stop(s:current_timer)
  endif
  let s:current_timer = timer_start(2000, 'DisableHL')
endfunction


nnoremap <silent> n n:call ResetTimer()<CR>
  • Unfortunately neovim (which I use) doesn't yet support this, so I can't test your solution, but as soon as I'll be able to I'll accept it if it would work. Right now mapping seems suspicious. Shouldn't you also map ResetTimer on N and where is where is does this timer start? – user1685095 Jun 3 '16 at 18:33
  • Of course, but since it's the same for each one, I included only one – nobe4 Jun 3 '16 at 18:34
  • Okay, I've finally had time to test this. This solution have many problems starting with that it shouldn't set nohlsearch, because than next search wouldn't be highlighted and occasionally instead of n in insert mode I get n:call or something like that, but thanks for example of working with timers. – user1685095 Nov 10 '16 at 8:15

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.