What is fzf?

fzf is a general-purpose command-line fuzzy finder written in Go. fzf is a fuzzy finder for your terminal, it is a command line application that filters each line from given input with a query that the user types. When the query changes, the results update in realtime.

Vim

Since I spend a lot of my time in Vim trying to find a file either by name or by some code inside a certain file. Streamlining that process is very important. Every context switch you have to make adds overhead and the possibility of losing focus of what you are trying to find. Therefore it should be as easy as possible, e.g: press a key, type query, press enter to go to matching file.

Finding files wasn’t too much of an issue here. There is a long list of Vim plugins that offer file searching using fuzzy matching or MRU algorithms. Two examples of this are CtrlP and Command-T. I used to be a fan of CtrlP which always managed to do the job.

fzf.vim

fzf has a small builtin Vim interface that already works, but it comes without any existing functionality. The author of fzf also wrote this fzf.vim plugin. It is a small wrapper that provides common functionality. This includes listing files, buffers, tags, git logs and much more!

Fuzzy searching in directories

Coming from CtrlP the first thing I needed was a replacement for fuzzy-finding files. The solution was to use the :Files command provided by fzf.vim. This lists files using your $FZF_DEFAULT_COMMAND environment variable. It opens the currently highlighted file on enter/return

The problem is that when you return a new file in current buffer it changes :Files directory. I am not sure about the background so maybe :cd or :lcdbut I don’t like it. I would like to use some “IDE-like” feature: it finds the project root —if it has .git, .svn, etc, and yet act on current directory if it’s not under a project.

function! s:find_git_root()
  return system('git rev-parse --show-toplevel 2> /dev/null')[:-2]
endfunction

command! ProjectFiles execute 'Files' s:find_git_root()

So this solves our issue. Since I am familiar with <C-p> keybing, I would like to use this binding:

map <C-p> :ProjectFiles<CR>

Vim is again “the most efficient” editor in a developer’s toolbox.

Links: