fzf是一个通用的命令行模糊查找器, 通过输入模糊的关键词就可以定位文件或文件夹。结合其他工具(比如rg)可以完成非常多的工作,在工作中可以大幅提高你的工作效率。
fzf可以用于文件、命令历史记录、进程、主机名、书签、git提交等。
1. fzf使用
1.1 安装
1 | git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf |
1.2 使用
安装后, 可以执行下fzf
, 先体验下, 另外 fzf 重写了 ctrl+r
搜索历史命令
1 | vim $(fzf) # 搜索后, 回车直接用 vi 打开 |
搜索过程中, CTRL-J 和 CTRL-K 向上翻和向下翻
bash和zsh的模糊完备, 默认触发是
**
, 例如:vim **<TAB>
, 或cd **<TAB>
, 或ssh **<TAB>
, 简直好用到飞起.一边查一边预览
1
fzf --preview 'cat {}'
可以配合管道使用
1
2
3ps -ef | fzf
seq 100 | fzf
history | fzf
1.3 搜索语法
Token | Match type | Description |
---|---|---|
sbtrkt | fuzzy-match | Items that match sbtrkt |
'wild | exact-match (quoted) | Items that include wild |
^music | prefix-exact-match | Items that start with music |
.mp3$ | suffix-exact-match | Items that end with .mp3 |
!fire | inverse-exact-match | Items that do not include fire |
!^music | inverse-prefix-exact-match | Items that do not start with music |
!.mp3$ | inverse-suffix-exact-match | Items that do not end with .mp3 |
1.4 和tmux结合
fzf 安装后自带一个 fzf-tmux, 但是会新开一个 panel ,并不是很好用, 建议以弹窗形成弹出
参考: https://www.liuvv.com/p/1104a363.html#3-fzf-tmux
1.5 打开文件
zsh 增加以下函数, ctrl-o 用 open 打开, ctrl-e 用 vim 打开
1 | # Modified version where you can press |
1.6 切换目录
zsh 增加以下函数
1 | # cd to selected directory |
1.7 搜索文件内容
zsh 增加以下函数, 需要配合 rg
命令
1 | #find-in-file - usage: fif <searchTerm> |
2. vim使用fzf
2.1 安装
1 | Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } "极限搜索文件 |
2.2 使用
:Files
:Rg