Customize a Fast Search with Fuzzy Finder for Vim
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 built-in 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.
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:
" Searching
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
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:
" FZF key bindings
nnoremap <C-f> :FZF<CR>
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-i': 'split',
\ 'ctrl-v': 'vsplit' }
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:
bindkey -M viins '^R' history-incremental-search-backward
bindkey -M vicmd '^R' history-incremental-search-backward
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
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:
export FZF_DEFAULT_COMMAND='find . -name .git -prune -o -name node_modules -prune -o -name coverage -prune -o -name tmp -prune -o -type f -print'
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.
This article was originally published by Noah Settersten at headway.io.
Did you like this article?
You can get more articles like this one about business & product strategy, design, and development right into your inbox through The Manifest.
More reads from our crew
Design
What is Gamification and How Can You Use It to Increase Product Engagement?
Tips for New Product Designers Without Design Experience
Using the Rules of Typography to Improve Your Designs
Development
React Native is Getting Evicted by Airbnb: Our Response
React Native: Customizing ESLint for a Specific Directory
How to use Trix and Shrine for WYSIWYG Editing with Drag-and-Drop Image Uploading
Strategy
Apple App Store Revenue: Keep a Bigger Piece of the Pie with Web-Based Purchases
Marketing
Focus on the Emotions That Motivate Customers to Use Your Product
Stop Confusing Your Customers: Be More Clear with Website Copy and Guide Them to Purchase