Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Vimscript function to make searching slightly more visible
"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
let g:waswrap = 0
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
"This sets the autocommand CursorHold to execute after 1ms, calling DoSearch
setl updatetime=1
augroup flicker_command
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
let c = nr2char(getchar())
"revert wrapping to previous state
if g:waswrap
set wrap
"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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment