public

Vimscript function to make searching slightly more visible

  • Download Gist
Vimscript search flicker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
"remap the searching commands to my new function. The function is called first, then immediately calls itself
"after the search is finished. This allows me to get the advantage of incsearch being unimpeded by anything
noremap <space> :call DoSearch('search')<Cr>/
noremap n :call DoSearch('search')<Cr>n
noremap N :call DoSearch('search')<Cr>N
noremap * :call DoSearch('search')<Cr>*
noremap # :call DoSearch('search')<Cr>#
noremap ? :call DoSearch('search')<Cr>?
 
"DoSearch takes a 'command' that specifies whether it needs to call itself again after exeution
function! DoSearch(command)
"if it has been called with 'search'
if a:command == 'search'
"remember wrap set/not set, turn off wrapping
if &wrap
let g:waswrap = 1
else
let g:waswrap = 0
endif
set nowrap
"set highlighting and our cursorline/column, since cursor not visible during function execution
"my highlighting for cursorline is light grey
set hls
hi CursorLine cterm=underline ctermbg =0
hi CursorColumn cterm=NONE ctermbg=0
set cursorline cursorcolumn
redraw
"This sets the autocommand CursorHold to execute after 1ms, calling DoSearch
setl updatetime=1
augroup flicker_command
au!
au CursorHold * call DoSearch('iterate')
augroup end
elseif a:command == 'iterate'
"kill the autocommand and set the updatetime to normal setting
setl updatetime=4000
au! flicker_command
augroup! flicker_command
augroup end
"get a character from the user
let c = nr2char(getchar())
"while it is one of the search forward/back commands, just keep searching forward
while c =~ '\v^(n|N)$'
execute "normal! ".c
redraw
let c = nr2char(getchar())
endwhile
"revert wrapping to previous state
if g:waswrap
set wrap
endif
"if we want to do any other key, temporarily turn off highlighting (so I can use :%s)
let str = ":nohls\<Cr>"
call feedkeys(str)
call feedkeys(c) "pass the user's non-searching key to vim
set nocursorline nocursorcolumn
endif
endfunction

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.