?>

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
  1. nmap <Leader>t :Files<CR>
  2. nmap <Leader>b :Buffers<CR>

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

There are no Comments, Be the First!

Have your say: