shell默认命令替代、fzf
命令行模糊搜索工具:fzf
。
简介 🧐
- 项目地址: https://github.com/junegunn/fzf
安装 🤭
- macOS (Homebrew):
brew install fzf
- Arch Linux:
sudo pacman -S fzf
- Debian/Ubuntu:
sudo apt install fzf
- Fedora:
sudo dnf install fzf
- Windows (Winget):
winget install fzf
- Cargo:
cargo install fzf
重要: 安装完成后,请运行以下命令来启用强大的快捷键集成(如 Ctrl-R
, Ctrl-T
):
$(brew --prefix)/opt/fzf/install # macOS
/usr/share/fzf/install # Linux (路径可能不同)
使用 😧
参数选项
❯ fzf --help
fzf [OPTIONS]Search-x, --extended Enable extended-search mode+x, --no-extended Disable extended-search mode-e, --exact Enable exact-match-i Case-insensitive match (default)+i Case-sensitive match--literal Do not parse query as a regular expression--algo=TYPE Fuzzy matching algorithm (v1|v2) (default: v2)-n, --nth=N[,..] Comma-separated list of fields to search in--with-nth=N[,..] Transform the presentation of each line using fzf's syntax-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)Interface-m, --multi Enable multi-select with TAB or Shift-TAB--no-mouse Disable mouse support--preview=COMMAND Command to generate preview for the focused line--preview-window=OPT Preview window layout (default: right:50%)--height=HEIGHT[%] Display fzf window below the cursor with the given height--[no-]border Draw border around the finder--color=... Color configuration--[no-]info Display finder info--prompt=STR Input prompt (default: '> ')Scripting-q, --query=STR Start the finder with the given query-1, --select-1 Automatically select the only match-0, --exit-0 Exit immediately when there's no match-f, --filter=STR Filter mode. Do not start interactive finder.--print-query Print query as the first lineOther-h, --help Display this help message-V, --version Display version information
案例 🤓
fzf
最强大的地方在于它和 shell 的深度集成。
-
搜索命令历史 (
Ctrl-R
):
这是fzf
最受欢迎的功能。按下Ctrl-R
会弹出一个交互式窗口,实时模糊搜索你过去输入的所有命令,比 shell 自带的搜索好用无数倍。 -
搜索文件和目录 (
Ctrl-T
):
在命令行中输入任何命令(如vim
,cat
,cd
)时,按下Ctrl-T
,会打开一个文件浏览器,你可以模糊搜索当前目录下的文件和文件夹,选中后会自动补全到你的命令行上。 -
快速切换目录 (
Alt-C
):
按下Alt-C
(在 macOS 上通常是Esc-C
),会列出你最近访问过的目录,模糊搜索后回车即可快速cd
过去。 -
自定义管道操作 (例如:快速杀死进程):
你可以将任何命令的输出通过管道传给fzf
来进行筛选。例如,下面这个命令可以让你交互式地选择并杀死一个进程:ps -ef | fzf -m | awk '{print $2}' | xargs kill -9
-
Git 集成 (示例:切换分支):
快速预览并切换 Git 分支:git branch | fzf | xargs git checkout
Refences 😜
- https://github.com/junegunn/fzf