Boosting CLI Productivity: An fzf Guide (Linux/macOS)
Install and use fzf as your terminal fuzzy finder: files, history, processes, Git, previews, bindings, and practical workflows.
Boosting CLI Productivity: An fzf Guide (Linux/macOS)
fzf is a general-purpose fuzzy finder. It brings “search + select” to the terminal: files, history, branches, processes, SSH targets—fast and interactive.
Installation
# macOS (Homebrew)
brew install fzf
$(brew --prefix)/opt/fzf/install # 一键启用按键绑定与补全
# Ubuntu/Debian
sudo apt install fzf
# Other Linux distros
git clone --depth 1 https://github.com/junegunn/fzf ~/.fzf
~/.fzf/install
Run the install script to enable key bindings and completion. Common defaults:
- Ctrl-R: search command history
- Ctrl-T: insert selected file path into the command line
- Alt-C: fuzzy cd into a directory
Quick Start
Find files (faster with ripgrep):
rg --files | fzf # list files then fuzzy search
rg -n "keyword" | fzf # filter search results
Batch operations with xargs:
rg --files | fzf -m | xargs -I{} echo {}
History / Processes / Git
-
History: press Ctrl-R, or run explicitly:
fc -ln 0 | fzf | pbcopy # macOS clipboard fc -ln 0 | fzf | xclip -sel clip -
Kill processes:
ps aux | fzf -m | awk '{print $2}' | xargs kill -
Git: branches, commits, files
# switch branches git branch | sed 's/* //' | fzf | xargs git switch # pick commits for cherry-pick git log --oneline | fzf -m | awk '{print $1}' | xargs git cherry-pick # open a changed file git diff --name-only | fzf | xargs -I{} ${EDITOR:-vim} {}
SSH / Service Ops
Jump via a host list:
cat ~/.ssh/hosts | fzf | xargs -I{} ssh {}
Or select a Kubernetes pod:
kubectl get pods -o name | fzf | xargs kubectl logs -f
Useful Flags
-m: multi-select--preview '<cmd> {}': preview pane ({}is the current item)--bind 'enter:execute(<cmd> {})': run a command on enter--height 40% --layout=reverse: compact layout with results at the bottom
Example: preview Markdown and open with your editor
rg --files | fzf --preview 'bat --style=plain --color=always {}' \
--bind 'enter:execute(${EDITOR:-vim} {+})+abort' -m
Handy Functions / Aliases
Add to ~/.zshrc or ~/.bashrc:
fd() { find "${1:-.}" -type f | fzf; }
fcd() { cd "$(fd "${1:-.}" | xargs -I{} dirname {} | fzf)"; }
fkill() { ps aux | fzf -m | awk '{print $2}' | xargs kill; }
fgrep() { rg -n "$1" | fzf; }
Wrap-up
Start with files and history, then extend with previews, binds, and multi-select. fzf quickly becomes the interactive command router for your terminal workflow.