Jump to content

How to Use fzf: Examples and Usage Tips

From RidgeRun Developer Wiki

fzf: Command-Line Fuzzy Finder

fzf is a general-purpose command-line fuzzy finder that helps you search and select items interactively from a list, such as files, directories, command history entries, processes, Git objects, and more. Instead of requiring exact matches, it uses fuzzy matching, so you can type partial or approximate text and still quickly find what you need. It is especially useful for reducing repetitive typing, speeding up navigation, and creating efficient shell workflows around existing command-line tools.

Installation

There are multiple ways to install fzf, depending on your operating system and environment.

Install using a package manager

# Debian/Ubuntu
sudo apt install fzf

# Fedora
sudo dnf install fzf

# Arch Linux
sudo pacman -S fzf

# macOS with Homebrew
brew install fzf

# Windows with winget
winget install junegunn.fzf

Install from the official repository

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

The install script can enable shell integration such as key bindings and completion for supported shells.

Basic Usage

The core idea behind fzf is simple:

producer_command | fzf | consumer_command
fzf in terminal (Taken from https://github.com/junegunn/fzf)
  • The producer generates a list (files, history, processes, etc.)
  • fzf lets you interactively filter and select
  • The consumer uses the selected result

Minimal example

fzf

Expected behavior:

- An interactive full-screen interface appears
- You type to filter results
- Use arrow keys or typing to narrow matches
- Press Enter to select
- The selected line is printed to stdout

File selection example

find . -type f | fzf

Expected output:

The selected file path is printed after exiting fzf

Using the selected result

nano "$(find . -type f | fzf)"

Expected behavior:

- fzf opens and lets you choose a file
- After selection, nano opens that file

Common Patterns

Instead of memorizing many commands, reuse these patterns:

Add preview

find . -type f | fzf --preview 'cat {}'

Expected behavior:

- Left side: selectable file list
- Right side: live preview of selected file

Use with command history

history | fzf

Use with search results

grep -R "TODO" . | fzf

Example output:

./main.c:42: // TODO: fix this

Multi-selection

find . -type f | fzf --multi

Expected behavior:

- Press Tab to select multiple entries
- Press Enter to confirm
- Outputs multiple lines (one per selection)

Using fzf with Git

Browse commit history

git log --oneline | fzf

Expected output:

List of searchable commits

You can search commits by message or hash interactively.

Preview full commit details

git log --oneline | fzf --preview 'git show {1}'

Expected behavior:

- Left side: commit list (short hash + message)
- Right side: full commit details (diff, author, date)

This is significantly more efficient than repeatedly running git show manually.

Show commits from all branches

git log --all --pretty=format:'%h %d %s' | fzf --preview 'git show {1}'

Expected behavior:

- Includes commits reachable from all branches and refs
- Useful when the current HEAD does not include newer commits from another branch tip

Checkout a selected commit

git checkout "$(git log --oneline | fzf | awk '{print $1}')"

Expected behavior:

- Select a commit from the list
- The repository switches to that commit (detached HEAD)

Checkout a commit from any branch

git checkout "$(git log --all --pretty=format:'%h %d %s' | fzf | awk '{print $1}')"

Expected behavior:

- Shows commits from all branches (not just current HEAD)
- Allows selecting commits that are not in the current history
- Checks out the selected commit in detached HEAD state

Tips and Tricks

Open found files directly with editors

The following commands make fzf part of a practical workflow where it opens the file on one of these editors after finding it:

vi "$(fzf --preview 'cat {}')"
nano "$(fzf --preview 'cat {}')"
code "$(fzf --preview 'cat {}')"

Expected behavior:

- fzf opens and lets you search for a file
- A preview of the file is shown on the side
- After selection, the chosen editor opens the file

Use syntax-highlighted previews with bat

The default preview command cat {} is fast but does not provide syntax highlighting. A better alternative is bat (or batcat on some systems), which adds colors and formatting.

fzf --preview 'bat --color=always --style=numbers --line-range=:300 {}'

On Debian-based systems, the command may be batcat instead:

fzf --preview 'batcat --color=always --style=numbers --line-range=:300 {}'

Expected behavior:

- Preview pane shows syntax-highlighted content
- Line numbers are displayed
- Output updates quickly as you move through results

Understand what {} means

{} is replaced with the currently selected item

Example:

--preview 'cat {}'

means:

cat <selected-file>

Avoid inefficient repetition, create aliases

Instead of repeating the same pattern:

find . -type f | fzf

define a reusable helper:

alias ffiles='find . -type f | fzf'

Prefer safe quoting

vim "$(fzf)"

This avoids issues with spaces or special characters in filenames.

Know the limits of preview

Good preview targets:
- source code
- shell scripts
- configuration files
- logs

Less suitable preview targets:
- binary files
- media files
- extremely large files

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.