Blazing Fast Fuzzy Find in Vim
Many people are surprised when they find out I use VIM as my primary editor. While vim seems like a ‘basic’ editor, it can have IDE like features added through its massive plugin community. I recently upgraded how I do fuzzy file navigation and find in project, and wanted to share how you can get blazing fast search in vim.
Fuzzy Find
First up is fzf . fzf
is a general-purpose command-line fuzzy finder. It lets you fuzzy find over any input that is piped into it. If you don’t provide input, it uses find
to search the current directory & subtree. Since I’m on macOS most of the time, I install fzf
with brew install fzf
. I then added the vim-fzf to my vim bundles. This plugin makes customizing fzf
easier. I then updated my .vimrc
with:
- set rtp+=/usr/local/opt/fzf
- let g:fzf_tags_command = 'ctags --extra=+f -R'
- let g:fzf_colors =
- \ { 'fg': ['fg', 'Normal'],
- \ 'bg': ['bg', 'Normal'],
- \ 'hl': ['fg', 'Comment'],
- \ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
- \ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
- \ 'hl+': ['fg', 'Statement'],
- \ 'info': ['fg', 'PreProc'],
- \ 'prompt': ['fg', 'Conditional'],
- \ 'pointer': ['fg', 'Exception'],
- \ 'marker': ['fg', 'Keyword'],
- \ 'spinner': ['fg', 'Label'],
- \ 'header': ['fg', 'Comment'] }
These settings make fzf
use my colour scheme colours, and connect it to ctags
. Finally, I mapped ,b
and ,t
to fzf
commands:
At this point, we’ve got pretty fast file navigation, but it includes icky files like .pyc
files and the contents of .git
. To fix this and get even more speed, we’ll use another tool.
The Silver Searcher
The Silver Searcher is another command like tool like grep
or ack
. Like ack
it is optimized for searching code, and is orders of magnitude faster than ack
due to being implemented in C. First, install the silver searcher with brew
:
- brew install the_silver_searcher
Next make sure ack.vim is installed in your vim bundles. After that, we’ll need to tell both fzf
and vim about ag
. In our ~/.bash_profile
add the following:
- export FZF_DEFAULT_COMMAND='ag --ignore *.pyc -g ""'
- export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
Next, let ack.vim
know that it should be using ag
:
- let g:ackprg = 'ag --nogroup --nocolor --column'
- map <Leader>a :Ack<Space>
You should now have not only a blazing fast file navigator, but also warp speed find in project via ,a
. As always, I keep my full vim configuration on github .
Hi,
do you have output when quitting vim after having used the searcher?
I have the output of the search results in my iTerm2 after quitting vim and was wondering if this is normal or a setting.
Thanks in advance.
Kr,
Bert
Bert on 4/16/19
Bert: I don’t have any search output in my terminal when I quit vim. Sounds like you might have something misconfigured.
mark story on 5/18/19