Skip to content

Instantly share code, notes, and snippets.

@tommcdo
Created December 20, 2013 16:57
Show Gist options
  • Save tommcdo/8057844 to your computer and use it in GitHub Desktop.
Save tommcdo/8057844 to your computer and use it in GitHub Desktop.
""" Fuzzy search
function! Fuzzy(text)
call FuzzyClear()
let text = '\c'.substitute(a:text, '.\zs', '.\\{-}', 'g')[:-6]
let b:fuzzy_last = text
call search(text)
call FuzzyHL(a:text)
let @/ = text
execute "nmap n :<C-U>call FuzzyN('n', '".escape(a:text, "'")."')<CR>"
execute "nmap N :<C-U>call FuzzyN('N', '".escape(a:text, "'")."')<CR>"
endfunction
function! FuzzyHL(text)
let l:pos = [line("."), col(".")]
let b:fuzzy_matches = []
let l:pattern_accum = '\c\%'.l:pos[0].'l\%'.l:pos[1].'c'
for l:i in range(0, strlen(a:text) - 1)
let l:pattern = l:pattern_accum.'\zs'.a:text[l:i].'\ze'
let b:fuzzy_matches += [matchadd('Search', l:pattern)]
let l:pattern_accum .= a:text[l:i].'.\{-}'
endfor
endfunction
function! FuzzyClear()
if exists('b:fuzzy_matches')
for l:match in b:fuzzy_matches
call matchdelete(l:match)
endfor
unlet b:fuzzy_matches
endif
endfunction
function! FuzzyCancel()
call FuzzyClear()
nunmap n
nunmap N
endfunction
function! FuzzyN(type, text)
call FuzzyClear()
silent! execute 'normal! '.a:type
if @/ !=# b:fuzzy_last
call FuzzyCancel()
else
call FuzzyHL(a:text)
endif
endfunction
command! -nargs=* Fuzzy call Fuzzy('<args>')
nnoremap z/ :<C-U>Fuzzy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment