我最喜欢的工具fzf
模糊搜索神器fzf;
fzf是一个交互式的搜索工具,他从标准输入读取一个候选列表,并将选择项写入标准输出。
你在任何地方用到搜索或者从多个候选项中选择一个时,都应该想到fzf,用fzf增强原来的搜索选择,让你爽的飞起!
安装
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install
在mac中使用brew时
brew install fzf # To install useful key bindings and fuzzy completion: $(brew --prefix)/opt/fzf/install
第二行别忘了执行了,不然没有常用的快捷键和自动补全功能
几个小样例
交互式搜索文件
find * -type f | fzf > selected
搜索文件并使用vim打开
vim $(fzf)
灵魂-简洁的搜索语法
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 |
命令行按键绑定和增强
快捷键 | 描述 |
ctrl+t | 搜索当前目录下的文件或者目录并打印到命令行中 |
ctrl+r | 将原来的历史命令搜索增强 |
alt+c | 选择目录并切换到选择的目录 |
模糊补全
# Files under the current directory # - You can select multiple items with TAB key vim **<TAB> # Files under parent directory vim ../**<TAB> # Files under parent directory that match `fzf` vim ../fzf**<TAB> # Files under your home directory vim ~/**<TAB> # Directories under current directory (single-selection) cd **<TAB> # Directories under ~/github that match `fzf` cd ~/github/fzf**<TAB>
还有我不常用的,进程号搜索,环境变量设置,host搜索
搜索并预览文件内容
然后在.zshrc里用函数或别名
alias tt='fzf --preview '"'"'[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (bat --style=numbers --color=always {} || rougify {} || highlight -O ansi -l {} || coderay {} || cat {}) 2> /dev/null | head -500'"'"
git命令增强
切换git分支
alias gcof='git checkout ${$(git branch --remote | fzf)#"origin/"}'
merge指定分支内容
alias gmf='git merge ${$(git branch --remote | fzf)#"origin/"}'
常见问题
mac的 apple silicon(m1,m2,m3)安装完fzf后,ctrl+r等常用绑定不生效
如果使用的是brew安装的fzf
在~/.zshrc中增加(注意路径中的版本)
source /opt/homebrew/Cellar/fzf/0.54.0/shell/key-bindings.zsh source /opt/homebrew/Cellar/fzf/0.54.0/shell/completion.zsh
参考:https://unix.stackexchange.com/questions/665689/fzf-ctlr-r-not-triggering-history-search-on-command-line
参考
官方git:https://github.com/junegunn/fzf
https://segmentfault.com/a/1190000011328080
https://www.jianshu.com/p/b48131e4ad06
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律