我最喜欢的工具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)

灵魂-简洁的搜索语法

TokenMatch typeDescription
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

 

posted @ 2022-05-29 21:50  halu126  阅读(239)  评论(0编辑  收藏  举报