Finding a Needle in a Haystack with FZF
There are a handful of fuzzy find tools, either from builtin editor features or via the shell, but they often suffer from unfortunate limitations. Whether slow performance or having cache results get out of date, they often require accepting tradeoffs. With shell bindings and the ability to tie into Vim, it’s sped up our workflow at Headway quite a bit. Searching for a file by name in a project is nearly instantaneous, and it’s easy to pull up a command in your shell history with a few keystrokes.Here’s how I’m using it in my environment.
A fast and customizable fuzzy finder for Vim
Trying to find specific items in your codebase
As a developer, I’m often bouncing between numerous files, looking for a specific item in a massive codebase, or trying to remember that arcane command I ran once months ago.
There are a handful of fuzzy find tools, either from builtin editor features or via the shell, but they often suffer from unfortunate limitations. Whether slow performance or having cache results get out of date, they often require accepting tradeoffs.
FZF - quickly find what you need in Vim
Enter FZF, a modern fuzzy finder written in Go that is not only incredibly fast but also very customizable.
With shell bindings and the ability to tie into Vim, it’s sped up our workflow at Headway quite a bit. Searching for a file by name in a project is nearly instantaneous, and it’s easy to pull up a command in your shell history with a few keystrokes.
Here’s how I’m using it in my environment:
Installing and managing FZF for Vim
I manage my Vim plugins using vim-plug which is a simple manager that installs plugins in parallel. It also has the ability to run post-install hooks to manage external libraries. Using it to install and manage FZF is straightforward:
In your .vimrc:
Running :PlugInstall in Vim will then copy FZF into the .fzf directory in your home folder and then run its post-install script to configure the native executable. FZF comes with basic Vim bindings out of the box, but I like to make a few changes:
In your .vimrc:
This maps the FZF search to Ctrl-F, and sets up hotkeys for opening splits in Vim similar to others I’m used to using. With only those few lines, searching for any file within the root directory becomes a breeze.
Zsh history search
When vim-plug installs FZF, it also makes it available for use on your PATH as well. With a few entries in Zsh’s config, we can tie command history search into FZF:
In your .zshrc:
This sets up Zsh’s history search on Ctrl-R and then runs the FZF zshell script to configure FZF for it.
Customize FZF - ignoring specific directories
Now that we have our fuzzy find in place, there’s a good chance we’ll want to exclude certain entries from the results. Library dependencies, the .git directory, and others can be easily left out by setting the FZF_DEFAULT_COMMAND environment variable:
In your .zshrc:
Add any other directories you want ignored with -o -name directory_name -prune.
Conclusion
Simple tools can make all the difference in being productive and covering ground quickly. I’ve found FZF to be a huge asset towards that goal, and it’s worth the investment to get a handle on it.