Vim fzf - Project Root
What is fzf?
fzf is a general-purpose command-line fuzzy finder written in
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.
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
plugins that offer file searching using fuzzy matching or MRU algorithms. Two
examples of this are
Command-T. I used to be a fan of
which always managed to do the job.
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
lists files using your
$FZF_DEFAULT_COMMAND environment variable. It opens the
currently highlighted file on
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
: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
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.