Skip to content

Drill: fzf Interactive Selection

Goal

Use fzf's interactive selection features — single select, multi-select, preview, and custom key bindings — to build efficient selection workflows from the command line.

Setup

  • Install fzf (apt install fzf, brew install fzf, or git clone from GitHub)
  • Create test data: mkdir -p /tmp/fzf-drill && seq 1 100 | xargs -I{} touch /tmp/fzf-drill/file-{}.txt

Commands

Single-select a file:

ls /tmp/fzf-drill/ | fzf

Multi-select files (Tab to toggle, Shift+Tab to untoggle):

ls /tmp/fzf-drill/ | fzf --multi

Preview file metadata while selecting:

ls /tmp/fzf-drill/ | fzf --preview 'stat /tmp/fzf-drill/{}'

Use a custom prompt and header:

ls /tmp/fzf-drill/ | fzf --prompt="Pick a file> " --header="Use arrows, type to filter"

Select and act — delete selected files (with confirmation):

ls /tmp/fzf-drill/ | fzf --multi | xargs -I{} rm -i /tmp/fzf-drill/{}

Bind a key to an action (Ctrl+Y to copy selection to clipboard):

ls /tmp/fzf-drill/ | fzf --bind 'ctrl-y:execute-silent(echo {} | xclip -sel clip)'

Pipe process list through fzf and kill the selected process:

ps aux | fzf --header-lines=1 --prompt="Kill> " | awk '{print $2}' | xargs kill -15

Use fzf with find for recursive directory selection:

find /tmp/fzf-drill -type f | fzf --multi --preview 'head -5 {}'

What to Look For

  • Typing filters results fuzzy — f50 matches file-50.txt
  • Tab toggles selection in multi-mode; the count updates in the prompt
  • Preview pane updates live as you move through results
  • Exit code 0 = selection made, 1 = no match, 130 = user cancelled (Ctrl+C)

Common Mistakes

  • Forgetting --multi when you need to select more than one item
  • Not quoting {} in preview/execute commands when filenames have spaces
  • Using fzf on binary output or extremely large inputs without filtering first
  • Not checking the exit code in scripts — a cancelled fzf should not proceed

Cleanup

rm -rf /tmp/fzf-drill