Skip to main content

Dev tools and workflow · Beginner · 15 min

Search files and shell history interactively with fzf

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · fzf 0.75.0 (GitHub release) · fd 10.3 (Ubuntu package)

You will have fzf 0.75 wired into your shell for history search, file insertion and directory jumps, fed by fd with a preview pane, plus two small pickers that switch Git branches and stop processes.

On this page

Most time lost at the terminal goes on finding things: the long docker command you ran last Tuesday, the path to a file three folders down, the name of a branch you made a week ago. fzf is a fuzzy finder. You give it a list of lines, type a few letters in any order of closeness, and it narrows the list as you type and prints the line you pick. That one idea becomes a history search, a file picker and a branch switcher once you connect it to your shell.

In this tutorial you install fzf 0.75, practise its search syntax on a small throwaway project, turn on the three shell keys, make fd the source of file lists so .gitignore is respected, add a preview pane, and write two functions of your own. The finder is interactive, so each step also shows a fzf --filter command. Filter mode runs the same matcher without opening the finder and prints every match, best first, which lets you see exactly what fzf would offer.

All configuration goes into one file, ~/.config/fzf/fzf.bash, so it is easy to read, copy to another machine or remove.

Step 1 · Install fzf and fd

Ubuntu 26.04's own package is fzf 0.67, which supports everything here except a few newer options. To get 0.75, install the official release binary into ~/.local/bin. Install the helpers first. fd builds the file lists later, and procps provides ps, which most systems already have:

Bash
sudo apt update
sudo apt install -y curl ca-certificates git fd-find procps

Download the release for your processor, check it against the published checksums, and unpack the single fzf binary:

Bash
FZF_VERSION=0.75.0
ARCH=$(dpkg --print-architecture)
cd "$(mktemp -d)"
curl -fsSLO "https://github.com/junegunn/fzf/releases/download/v${FZF_VERSION}/fzf-${FZF_VERSION}-linux_${ARCH}.tar.gz"
curl -fsSLO "https://github.com/junegunn/fzf/releases/download/v${FZF_VERSION}/fzf_${FZF_VERSION}_checksums.txt"
grep " fzf-${FZF_VERSION}-linux_${ARCH}.tar.gz\$" "fzf_${FZF_VERSION}_checksums.txt" | sha256sum -c -
mkdir -p ~/.local/bin
tar -xzf "fzf-${FZF_VERSION}-linux_${ARCH}.tar.gz" -C ~/.local/bin fzf
cd ~

dpkg --print-architecture prints amd64 on most servers and laptops and arm64 on 64-bit ARM machines such as a Graviton server or a Raspberry Pi on a 64-bit system, and both have a release file. sha256sum prints OK next to the file name. If it prints FAILED, delete the download and try again. The file is a couple of megabytes, so it is cheap on prepaid data.

Ubuntu names the fd binary fdfind, because another package already owns fd. Link it under the usual name, and put ~/.local/bin on your PATH for this terminal (step 4 makes it permanent):

Bash
ln -sf "$(command -v fdfind)" ~/.local/bin/fd
export PATH="$HOME/.local/bin:$PATH"

On macOS, brew install fzf fd installs both. On an Android phone running Termux, pkg install fzf fd does the same.

A binary installed by hand gets no updates from apt. To upgrade, run the download block again with a newer FZF_VERSION from the releases page.

Check it worked

Bash
fzf --version
fd --version

The first line starts with 0.75.0. The second prints fd 10.3.0 on Ubuntu 26.04, or a newer version on macOS.

Step 2 · Make a practice project

Create a small Git project in ~/fzf-demo with source files, tests, docs, a dotfile and a build folder that Git ignores. You also write a short history file to search later, so you do not need your own history to follow along:

Bash
mkdir -p ~/fzf-demo
cd ~/fzf-demo
git init -q -b main
mkdir -p src/app tests docs build
printf 'def charge(amount):\n    return {"charged": amount}\n' > src/app/payments.py
printf 'def find_user(user_id):\n    return {"id": user_id}\n' > src/app/users.py
printf 'from app.payments import charge\n' > tests/test_payments.py
printf 'from app.users import find_user\n' > tests/test_users.py
printf '# Deploying\n\nRun the release script.\n' > docs/deploy.md
printf '# fzf demo\n' > README.md
printf 'DATABASE_URL=\n' > .env.example
printf 'build/\n' > .gitignore
printf 'console.log("bundled")\n' > build/bundle.js
git add .
git commit -q -m "Start the demo project"
cat > history.txt <<'EOF'
git status
docker compose logs -f api
ssh deploy@203.0.113.10
kubectl get pods -n payments
git push origin feature/payments-api
EOF

Check it worked

Bash
git log --oneline
fd --type f --hidden --exclude .git

Git shows one commit. fd lists nine files: .env.example, .gitignore, README.md, docs/deploy.md, history.txt, the two files under src/app and the two under tests. build/bundle.js is missing, because fd skips what .gitignore lists inside a Git repository.

Step 3 · Learn the search syntax

Run fzf on its own and it opens a finder over the files under the current folder. Type pay and the list shrinks to the lines that contain p, a and y in that order, with gaps allowed. Up and Down move, Enter prints the highlighted line, and Esc cancels.

Filter mode shows the same result without the finder. Pipe the file list in and give the query to --filter:

Bash
fd --type f --hidden --exclude .git | fzf --filter pay

This prints src/app/payments.py and tests/test_payments.py. Several words separated by spaces must all match. A few characters change how a word matches:

  • pay matches lines that contain p, a and y in that order (fuzzy).
  • 'pay matches lines that contain the exact text pay.
  • ^tests matches lines that start with tests.
  • .md$ matches lines that end with .md.
  • !test matches lines that do not contain test.
  • users | deploy matches lines that match users or deploy.

Try them:

Bash
fd --type f --hidden --exclude .git | fzf --filter '.md$'
fd --type f --hidden --exclude .git | fzf --filter 'pay !test'
fd --type f --hidden --exclude .git | fzf --filter 'users | deploy'

The first prints README.md and docs/deploy.md. The second prints only src/app/payments.py, because !test removes the test file. The third prints the two users files and docs/deploy.md. A query in lower case ignores case; add a capital letter and it becomes case-sensitive.

Check it worked

Bash
fzf --filter 'logs api' < history.txt

This prints docker compose logs -f api: the one history line that holds both words. That is the search Ctrl-R gives you in the next step.

Step 4 · Turn on the shell keys

fzf ships its key bindings inside the binary. fzf --bash prints a script that binds three keys:

  • Ctrl-R searches your shell history and puts the chosen command on the prompt, ready to edit or run. Press Ctrl-R again inside the finder to switch between best match first and newest first.
  • Ctrl-T picks one or more files (Tab marks several) and types their paths at the cursor, so `vim ` then Ctrl-T opens the file you pick.
  • Alt-C picks a folder and changes into it. On macOS, set your terminal to use Option as Meta, or press Esc then C.

Write the config file and load it from ~/.bashrc. This writes ~/.config/fzf/fzf.bash from scratch, so run it once; later steps add to the end of the file:

Bash
mkdir -p ~/.config/fzf
cat > ~/.config/fzf/fzf.bash <<'EOF'
# fzf setup, loaded from ~/.bashrc
case ":$PATH:" in
  *":$HOME/.local/bin:"*) ;;
  *) export PATH="$HOME/.local/bin:$PATH" ;;
esac
eval "$(fzf --bash)"
EOF
grep -qsF '.config/fzf/fzf.bash' ~/.bashrc || echo '[ -f ~/.config/fzf/fzf.bash ] && . ~/.config/fzf/fzf.bash' >> ~/.bashrc

The grep makes the last line safe to run twice: it adds the loader only if ~/.bashrc does not have it yet.

If your shell is zsh, put the same lines in ~/.config/fzf/fzf.zsh, with this line in place of the eval line, and load that file from ~/.zshrc instead:

Bash
source <(fzf --zsh)

Check it worked

Open a new terminal and press Ctrl-R. A list of your past commands appears with a prompt at the bottom; type part of a command and press Enter to put it on the prompt. To check from the current terminal, ask a fresh interactive shell whether the widgets exist:

Bash
bash -ic 'declare -F __fzf_history__ __fzf_select__ __fzf_cd__'

It prints the three function names, one per line. A warning about line editing or job control is harmless here.

Step 5 · Use fd as the file source

Without help, Ctrl-T and Alt-C use fzf's built-in folder walker. It is fast, but it knows nothing about .gitignore. Apart from .git and node_modules, which it always skips, it offers every file, including generated folders such as build and dist. Tell fzf to ask fd instead. --hidden keeps dotfiles such as .env.example, and --exclude .git drops Git's own folder:

Bash
cat >> ~/.config/fzf/fzf.bash <<'EOF'

# File and folder lists from fd: respects .gitignore, keeps dotfiles, skips .git
export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git --strip-cwd-prefix'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_ALT_C_COMMAND='fd --type d --hidden --exclude .git --strip-cwd-prefix'
EOF

FZF_DEFAULT_COMMAND is what plain fzf runs when nothing is piped into it. Ctrl-T and Alt-C each read their own variable, which is why all three are set.

Check it worked

Run the lists the keys now use, from inside the project:

Bash
cd ~/fzf-demo
bash -ic 'eval "$FZF_CTRL_T_COMMAND"' | fzf --filter bundle || echo "no match for bundle"
bash -ic 'eval "$FZF_ALT_C_COMMAND"'

The first prints no match for bundle, because the ignored build/bundle.js is no longer offered. The second lists docs/, src/, src/app/ and tests/, with no build/. fd prints folders with a trailing slash.

Step 6 · Add a preview pane

A preview shows the highlighted file before you pick it, which saves opening the wrong one. fzf runs the preview command each time the highlight moves, with {} replaced by the current line. Add default options and a preview for each key:

Bash
cat >> ~/.config/fzf/fzf.bash <<'EOF'

# Layout and previews. Ctrl-/ hides or shows the Ctrl-T preview.
export FZF_DEFAULT_OPTS='--height 50% --layout reverse --border'
export FZF_CTRL_T_OPTS="--preview 'head -n 40 {}' --preview-window 'right,60%,<80(down,50%)' --bind 'ctrl-/:toggle-preview'"
export FZF_ALT_C_OPTS="--preview 'ls -A {}'"
EOF

--height 50% opens the finder under the prompt instead of clearing the whole screen, and --layout reverse puts the query at the top. The preview sits on the right, and <80(down,50%) moves it below the list when the terminal is narrower than 80 columns, which keeps it readable on a small laptop or a phone running Termux. head -n 40 is used because it is on every system; if you install bat, a highlighting viewer, use bat --color=always {} instead.

Check it worked

Open a new terminal, cd ~/fzf-demo, type cat ` and press Ctrl-T. Move the highlight onto `src/app/payments.py and the pane shows its two lines. From the current terminal, confirm that fzf accepts the options, the same way the Ctrl-T key combines them, and run the preview command by hand:

Bash
cd ~/fzf-demo
bash -ic 'eval "$FZF_CTRL_T_COMMAND" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf --filter payments'
head -n 40 src/app/payments.py

The first command prints the two payments files; a mistyped option would make fzf stop with an unknown option error instead. The second prints the charge function.

Step 7 · Pick a Git branch to switch to

The keys are only the start. Any command that prints lines can feed fzf, and whatever you pick comes back on standard output for the next command. Add a function that lists your local branches, shows the last commits of the highlighted one, and switches to the branch you pick:

Bash
cat >> ~/.config/fzf/fzf.bash <<'EOF'

# fbr [query]: pick a local Git branch and switch to it
fbr() {
  local branch
  branch=$(git for-each-ref --format='%(refname:short)' refs/heads |
    fzf --header 'Switch to branch' --query "${1:-}" \
        --preview 'git log --oneline --max-count 10 {}') || return
  git switch "$branch"
}
EOF

If you press Esc, fzf exits with a non-zero status and || return stops before git switch runs. The optional argument fills in the query, so fbr pay opens the finder already narrowed down.

Make a few branches to try it on, and preview the list fbr will show:

Bash
cd ~/fzf-demo
git branch feature/payments-api
git branch fix/login-timeout
git branch docs/readme
git for-each-ref --format='%(refname:short)' refs/heads | fzf --filter pay

The filter prints feature/payments-api, the only branch that matches.

Check it worked

Open a new terminal, cd ~/fzf-demo, and run fbr pay. The finder shows feature/payments-api with the demo commit in the preview. Press Enter, then confirm:

Bash
fbr pay
git branch --show-current

Git reports Switched to branch 'feature/payments-api', and the second command prints feature/payments-api.

Step 8 · Pick a process to stop

The same pattern finds a process by name instead of by number. This function lists your own processes, hides fzf itself, and sends the picked process the polite TERM signal, which lets it clean up. It only ever stops the one line you choose, so read that line before you press Enter:

Bash
cat >> ~/.config/fzf/fzf.bash <<'EOF'

# fkill [query]: pick one of your processes and send it TERM
fkill() {
  local pid
  pid=$(ps -u "$(id -u)" -o pid=,comm=,args= | awk '$2 != "fzf"' |
    fzf --header 'Stop which process?' --query "${1:-}" |
    awk '{print $1}')
  [ -n "$pid" ] || return 1
  kill "$pid" && echo "Sent TERM to $pid"
}
EOF

ps -u "$(id -u)" lists only your processes, so the function never offers system services. Each line shows the process ID, the program name and its full command line, and the final awk keeps the ID. If a process ignores TERM, kill -9 <pid> forces it; use that only when TERM has failed.

Start a long-running command to practise on, and see what the picker will match:

Bash
sleep 4242 &
sleep_pid=$!
ps -u "$(id -u)" -o pid=,comm=,args= | awk '$2 != "fzf"' | fzf --filter 'sleep 4242'

The filter prints one line with the process ID, sleep and sleep 4242.

Check it worked

In your own terminal, run fkill 'sleep 4242', check that the highlighted line is the sleep process and press Enter. Then make sure it has gone:

Bash
fkill 'sleep 4242'
wait "$sleep_pid" || echo "sleep ended with status $?"

The function prints Sent TERM to and the process ID, and the second line reports status 143, which means the process ended on TERM.

Step 9 · Clean up

Remove the practice project. The fzf setup in ~/.config/fzf/fzf.bash stays, because that is the point of the tutorial:

Bash
cd ~
rm -rf ~/fzf-demo

To remove fzf completely later, delete the line that loads ~/.config/fzf/fzf.bash from ~/.bashrc in your editor, then run rm -r ~/.config/fzf ~/.local/bin/fzf ~/.local/bin/fd.

Check it worked

Bash
test -d ~/fzf-demo || echo "practice project removed"

It prints practice project removed.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.