If you spend more than two hours a day in the terminal, you know the frustration of trying to find a specific file in a deeply nested directory or searching through a massive command history. For years, I relied on a combination of find, grep, and a lot of mindless tabbing. Then I discovered fzf.
In this fzf command line tutorial, I’m going to show you how to integrate a general-purpose fuzzy finder into your workflow. Unlike standard search tools, fzf doesn’t require you to remember the exact name of a file; it finds the closest match based on what you type in real-time. It’s one of those tools that, once installed, makes every other modern unix command feel incomplete.
Prerequisites
Before we dive in, ensure you have the following installed on your system:
- A Unix-like terminal (macOS, Linux, or WSL2 on Windows).
- A shell like Bash or Zsh (I personally use Zsh with Oh My Zsh).
- Basic familiarity with piping (
|) in the command line.
Step 1: Installing fzf
Installing fzf is straightforward. Depending on your OS, I recommend using the official git installation method because it includes the useful shell extensions (keybindings and auto-completion).
# Clone the repository into your home directory
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
# Run the install script
~/.fzf/install
During the installation, the script will ask if you want to enable fuzzy completion and keybindings. Say yes to all of them. This is where the real magic happens.
Step 2: Mastering the Basics
Once installed, running fzf by itself will list all files in the current directory and let you filter them. But the real power comes from using it as a filter for other commands.
Filtering File Lists
Instead of scrolling through ls, pipe the output into fzf:
# Find a file and print its name
ls | fzf
Integrating with the Editor
In my daily setup, I rarely use vim filename. Instead, I use fzf to select the file first. Here is a simple alias I added to my .zshrc:
# Open a selected file in Vim
vim $(fzf)
As shown in the image below, this eliminates the need to type out long paths manually, allowing you to jump straight into the code.
Step 3: Using Built-in Keybindings
If you enabled the shell extensions during installation, you now have three game-changing shortcuts:
- CTRL-T: Search for a file in the current directory and paste the path onto your command line.
- CTRL-R: Search through your command history. This is a lifesaver when you remember a complex command from three weeks ago but can’t remember the exact flags.
- ALT-C: Fuzzy search for a directory and
cdinto it immediately.
Step 4: Advanced Integration & Automation
To truly optimize your productivity, you can combine fzf with other tools. For example, if you are using the gh cli workflow automation patterns, you can use fzf to pick which PR to checkout.
# Pick a PR number using fzf and checkout the branch
gh pr list | fzf | awk '{print $1}' | xargs -I {} gh pr checkout {}
Customizing the Preview Window
One of my favorite features is the preview window. By using the --preview flag, you can see the content of a file before you select it. I use bat (a cat clone with syntax highlighting) for this:
# fzf with a syntax-highlighted preview
fzf --preview 'bat --style=numbers --color=always --line-range :500 {}'
Pro Tips for Power Users
- Ignore Hidden Files: To prevent
.gitfolders from cluttering your results, set theFZF_DEFAULT_COMMANDenvironment variable to usefdorripgrep. - Multi-select: Use
fzf -mto select multiple files using the Tab key. - Custom Layouts: Use
--height 40% --layout=reverseto keep the search bar at the top and the list compact.
Troubleshooting Common Issues
Issue: Keybindings aren’t working.
Ensure you have sourced the fzf shell script in your configuration file. For Zsh, check if [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh is present in your .zshrc.
Issue: fzf is slow in huge directories.
If you are searching through a massive monorepo, the default find command can be slow. Install fd-find and add this to your shell config: export FZF_DEFAULT_COMMAND='fd --type f'.
What’s Next?
Now that you’ve mastered the basics of this fzf command line tutorial, I recommend exploring zoxide for smarter directory jumping or looking into my guide on modern unix commands to replace other legacy tools in your stack.