
You ran a long docker run command last week, with all the right flags and volumes, and now you need it again. You press the up arrow fifty times, give up, and start typing it from memory. Setting up fzf with Zsh fixes that. fzf is a fuzzy finder, which means you type a few letters from anywhere in what you want, and it narrows the list as you go.
In this guide, I’ll show you how to install fzf, hook it into your Zsh setup, and use it to search history, folders and files.
Install fzf
You can install fzf with your package manager (the quick way) or from the GitHub repository (the manual way). I’d start with the package manager unless you need the newest version.
Quick way: install fzf on macOS with Homebrew
On macOS, Homebrew handles everything. To install fzf, run:
brew install fzf
When it finishes, run fzf --version to check that it’s on your path.
Quick way: install fzf on Ubuntu with APT
On Ubuntu, fzf is in the standard repositories. To install it, run:
sudo apt-get install fzf
Same check as before: fzf --version should print a version number.
Manual way: install fzf from GitHub
The manual install is useful when your distro’s package is older than the features you want, or when you don’t have a package manager handy. To clone the repository and run the install script, run:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
The script asks a few questions and can set up key bindings and auto-completion for you. Say yes to both unless you want to wire them up by hand.
Configure fzf with Zsh
Configuring fzf with Zsh comes down to a few lines in your ~/.zshrc file, the config file Zsh reads every time it starts a new shell.
Turn on key bindings and auto-completion
To load fzf’s key bindings and completion in every shell, add this line to your ~/.zshrc:
# Enable fzf key bindings and auto-completion
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
This is what makes the shortcuts below work. Without it, fzf is installed but Zsh doesn’t know about it.
Search command history with Ctrl+R
To swap the default history search for fzf’s fuzzy search on Ctrl+R, add:
# Use fzf for command history search
bindkey '^R' fzf-history-widget
Now that week-old docker run command is a few letters away instead of fifty up-arrow presses.
Jump to folders with a custom cd function
To pick a folder from a fuzzy list instead of typing a long path, add this function:
# Use fzf for directory navigation
function cd() {
if [[ $# -gt 0 ]]; then
builtin cd "$@"
else
local dir
dir=$(find ${1:-.} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf +m) && builtin cd "$dir"
fi
}
After this, running cd on its own opens fzf so you can choose where to go.
Reload your Zsh config
To apply your changes without opening a new terminal, run:
source ~/.zshrc
If something looks off, check your ~/.zshrc for typos first. It’s usually a missing quote.
Use fzf in your daily work
Once fzf is set up, three habits cover most of what you’ll do with it.
Search your command history
Press Ctrl+R, type any part of the command you remember, and press Enter to drop it on your prompt. It’s fast even with a huge history file.
Move between directories
Type cd and press Enter. fzf pops up with a list of folders, and you fuzzy search your way to the one you want. No more typing out full paths.
Find files in the current folder
To search the files under your current directory, run:
fzf
You get a live list of files. Type to filter, use the arrow keys to move, and press Enter to select.
Add your own key bindings
You can bind fzf to any key for custom actions. To open a quick file search with Ctrl+F, add:
# Bind fzf to Ctrl+F for file search
bindkey '^F' 'fzf > /dev/tty'
Reload your config again, and Ctrl+F is ready. Play with a few bindings until fzf fits the way you work.
Where fzf shows up next
Once you’re used to fuzzy search, you’ll want it everywhere. Some tools pick it up on their own: kubectx and kubens, which I cover in 4 Kubernetes CLI tools I use to manage clusters faster, give you an interactive picker when fzf is installed. It also pairs nicely with tmux for terminal management if you live in split panes all day.
Got a favorite fzf trick? Drop it in the comments. Hope this saves you some time, and if you want your prompt to look as good as it now works, check out how to install Powerlevel10k with Oh My Zsh on Linux next. 🚀
Leave a Reply