Skip to content

Drill: fzf Integration Patterns

Goal

Use fzf for interactive fuzzy selection in common workflows: file selection, history search, git branches, and process killing.

Setup

  • Install fzf (apt install fzf, brew install fzf, or git clone from GitHub)
  • Shell integration enabled (source /usr/share/doc/fzf/examples/key-bindings.bash or equivalent)

Commands

Basic fuzzy file selection:

fzf

Preview files while selecting:

fzf --preview 'cat {}'

Open a selected file in an editor:

vim $(fzf --preview 'head -50 {}')

Search command history interactively (Ctrl+R with fzf integration):

history | fzf --tac --no-sort

Select and checkout a git branch:

git branch -a | fzf | xargs git checkout

Select a git commit to inspect:

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

Kill a process interactively:

ps aux | fzf --header-lines=1 | awk '{print $2}' | xargs kill

Search and cd into a directory:

cd $(find . -type d | fzf)

Multi-select files (Tab to select, Enter to confirm):

fzf --multi | xargs rm -i

Use fzf with ripgrep for code search:

rg --line-number . | fzf --delimiter=: --preview 'bat --color=always {1} --highlight-line {2}' | cut -d: -f1

What to Look For

  • fzf filters results as you type with fuzzy matching
  • Preview window shows file contents or command output for the highlighted item
  • Multi-select mode (--multi) allows selecting multiple items with Tab
  • Exit code 130 means the user cancelled (Ctrl+C); handle in scripts

Common Mistakes

  • Not enabling shell key bindings (Ctrl+R, Ctrl+T, Alt+C)
  • Piping binary or very large output into fzf, causing slow rendering
  • Forgetting --multi when you need to select more than one item
  • Not quoting the output of fzf when filenames contain spaces

Cleanup

No cleanup needed. fzf is an interactive filter with no side effects.