Enhanced hlsearch in Vim


A while back I watched the talk More Instantly Better Vim by Damian Conway from OSCON 2013.

There were a lot of interesting Vim stuff in that talk and the talk it self was entertaining (go watch it).

One of the things I took away from the talk was enhanced hlsearch. When there's a lot of matches it can be kind of hard to see what occurence you just jumped to. This snippet makes the current match flash for a brief period of time.

Unfortunately there's a drawback from using it since the implementations involves the use of sleep. It's not optimal but when fine tuned to serve its purpose without being annoying I found it quite useful.

Put the following snippet into your .vimrc and give it a spin.

" Highlight search matches and next
set hlsearch

" create a HLNext (from http://www.youtube.com/watch?v=aHm36-na4-4)
nnoremap <silent> n n:call HLNext(0.2)<cr>
nnoremap <silent> N N:call HLNext(0.2)<cr>
highlight WhiteOnRed ctermbg=white guibg=red
function! HLNext (blinktime)
  let [bufnum, lnum, col, off] = getpos('.')
  let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
  let target_pat = '\c\%#'.@/
  let blinks = 3
  for n in range(1,blinks)
    let red = matchadd('WhiteOnRed', target_pat, 101)
    redraw
    exec 'sleep ' . float2nr(a:blinktime / (2*blinks) * 1000) . 'm'
    call matchdelete(red)
    redraw
    exec 'sleep ' . float2nr(a:blinktime / (2*blinks) * 1000) . 'm'
  endfor
endfunction

...

roppert

Programmer, tinker, maker, writer, human. See roppert.se