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:

Show Plain Text
  1. set rtp+=/usr/local/opt/fzf
  2.  
  3. let g:fzf_tags_command = 'ctags --extra=+f -R'
  4. let g:fzf_colors =
  5. \ { 'fg':      ['fg', 'Normal'],
  6.   \ 'bg':      ['bg', 'Normal'],
  7.   \ 'hl':      ['fg', 'Comment'],
  8.   \ 'fg+':     ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
  9.   \ 'bg+':     ['bg', 'CursorLine', 'CursorColumn'],
  10.   \ 'hl+':     ['fg', 'Statement'],
  11.   \ 'info':    ['fg', 'PreProc'],
  12.   \ 'prompt':  ['fg', 'Conditional'],
  13.   \ 'pointer': ['fg', 'Exception'],
  14.   \ 'marker':  ['fg', 'Keyword'],
  15.   \ 'spinner': ['fg', 'Label'],
  16.   \ '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:

Show Plain Text
nmap t :Files nmap b :Buffers

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:

Show Plain Text
  1. 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:

Show Plain Text
  1. export FZF_DEFAULT_COMMAND='ag --ignore *.pyc -g ""'
  2. export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

Next, let ack.vim know that it should be using ag:

Show Plain Text
  1. let g:ackprg = 'ag --nogroup --nocolor --column'
  2. 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 .

Comments

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

Have your say: