跳至主要内容

fzf

fzf is a general-purpose command-line fuzzy finder.

模糊搜尋器,支援檔案、歷史、程序搜尋,是現代 CLI 工作流程的核心工具。

基本用法

檔案搜尋

# 搜尋當前目錄檔案
fzf

# 搜尋並開啟檔案
vim $(fzf)

# 搜尋並切換目錄
cd $(find . -type d | fzf)

歷史搜尋

# 搜尋命令歷史 (Ctrl+R)
history | fzf

# 搜尋並執行歷史命令
eval $(history | fzf | sed 's/^[0-9 ]*//')

進階技巧

預覽功能

# 檔案預覽
fzf --preview 'bat --color=always {}'

# 目錄預覽
find . -type d | fzf --preview 'ls -la {}'

多選模式

# 多選檔案 (Tab 選擇,Enter 確認)
fzf --multi

# 批次刪除檔案
rm $(fzf --multi)

Git 整合

# 搜尋 Git 分支
git branch | fzf

# 搜尋 Git 提交
git log --oneline | fzf

鍵盤快捷鍵

快捷鍵功能
Ctrl+R搜尋命令歷史
Ctrl+T搜尋檔案並插入路徑
Alt+C搜尋目錄並切換
Tab多選模式下選擇項目
Shift+Tab多選模式下取消選擇

配置

環境變數

# 預設選項
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'

# 預設命令 (使用 fd 取代 find)
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'

# Ctrl+T 的命令
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

實用 Alias

# 快速檔案編輯
alias vf='vim $(fzf)'

# 快速目錄切換
alias cdf='cd $(find . -type d | fzf)'

# 搜尋並殺死程序
alias fkill='kill $(ps aux | fzf | awk "{print \$2}")'

See Also