Skip to content

Instantly share code, notes, and snippets.

@cognitivegears
Created October 24, 2016 17:02
Show Gist options
  • Save cognitivegears/fc689776260b34b396a802af88de37cf to your computer and use it in GitHub Desktop.
Save cognitivegears/fc689776260b34b396a802af88de37cf to your computer and use it in GitHub Desktop.
Search Results Window with Unite and Anzu
" General Anzu configuration - change to taste
nmap n <Plug>(anzu-n-with-echo)
nmap N <Plug>(anzu-N-with-echo)
nmap * <Plug>(anzu-star-with-echo)
nmap # <Plug>(anzu-sharp-with-echo)
" clear status
nmap <Esc><Esc> <Plug>(anzu-clear-search-status)
" statusline
set statusline+=%{anzu#search_status()}
" Update the search status results while moving through the file
augroup anzu-update-search-status
autocmd!
autocmd CursorMoved *
\ :AnzuUpdateSearchStatus|echo anzu#search_status()
augroup END
" Stores the last seen search criteria
let g:saved_search = ''
" Clear the last search on VIM startup (so it won't start with a search window)
autocmd VimEnter * call ClearLastSearch()
function! ClearLastSearch()
let @/ = ''
endfunction
" This function monitors the last search results, and when it has changed
" closes the previous search window (if any) and starts a new search window.
autocmd CursorHold * call CheckSearch()
function! CheckSearch()
if g:saved_search != @/ && !has("vim_starting")
" Save the current search term
let g:saved_search = @/
" We are in a new search, update Anzu and close/open search list window
AnzuUpdateSearchStatus|echo anzu#search_status()
UniteClose searchList
" Change the prompt if your font doesn't have this
"Also, setting the width to 40, you may want to change this based on your terminal size.
Unite -buffer-name=searchList -prompt=🔎 -prompt-focus -immediately -auto-highlight -no-quit -winwidth=40 -no-truncate -vertical -no
-empty -no-focus anzu
endif
endfunction
" Clear highlighting and the search list on escape in normal mode
nnoremap <esc> :noh<return>:UniteClose searchList<return>:AnzuClearSearchStatus<return>:let @/ = ""<return><esc>
nnoremap <esc>^[ <esc>^[
" Close unite search results window if it is the last buffer left
autocmd BufEnter * if (winnr("$") == 1 && unite#get_unite_winnr("searchList") != -1)|q|endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment