fzf是目前最快的fuzzy finder。使用golang编写。结合其余工具(好比ag和fasd)能够完成很是多的工做。
让你经过输入模糊的关键词就能够定位文件或文件夹。当你的思惟也习惯了模糊匹配后,在工做中能够大幅提升你的工做效率。
模糊搜索的概念以下,你记得文件名含有con, te, go, 那么你只须要把全部文件送给fzf, 而后在窗口里输入con te go就能够了,无论实现名是test_continus_go仍是go_cont_test都会匹配上。git
使用gitgithub
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install
cd ~/.fzf && git pull && ./install
fzf默认会从STDIN读入数据,而后将结果输出到STDOUTgolang
find * -type f | fzf > selected
上面命令从find的搜索结果中读入,输出到文件selected中shell
在finder(输出交换窗口)里,vim
fzf默认全屏模式,你能够定制高度segmentfault
vim $(fzf --height 40%)
你能够经过$FZF_DEFAULT_OPTS来设定默认值ruby
export FZF_DEFAULT_OPTS='--height 40% --reverse --border'
fzf默认会以“extened-search"模式启动, 这种模式下你能够输入多个以空格分隔的搜索关键词, 如^music .mp3$
, sbtrkt !fire
.svg
Token | Match type | Description |
---|---|---|
sbtrkt | fuzzy-match | 匹配sbtrkt |
^music | prefix-exact-match | 以music开头 |
.mp3$ | suffix-exact-match | 以.mp3结尾 |
'wild | exact-match(quoted) | 精确包含wild |
!fire | inverse-exact-match | 不包含fire |
!.mp3$ | inverse-suffix-exact-match | 不以.mp3结尾 |
若是你不想用fuzzy match, 能够用fzf -e
作精确匹配
|能够作or匹配, 好比函数
^core go$|rb$|py$
表示以core开头,以go或rb或py结尾的工具
FZF_DEFAULT_ COMMAND
FZF_DEFAULT_OPTS
在命令行下按下ctrl-t会打开fzf窗口,若是你选中某个条目并按下Enter, 选中的条目会被拷贝到命令行上
若是想同时预览文件内容,可使用--preview
选项
export FZF_CTRL_T_OPTS="--preview '(highlight -O ansi -l {} 2> /dev/null || cat {} || tree -C {}) 2> /dev/null | head -200'"
也能够用--select-1
和--exit-0
前者是若是只有一个条目,那么自动选中并退出fzf
后者是若是条目为空,自动退出
上面两个选项对ALT-C也有用
在命令行下按下ctrl-r, fzf会列出history命令,选中条目并离开fzf的话, 选中条目会被拷到命令行上
在zsh下可使用下面的方法来按下C-XC-R来直接执行
fzf-history-widget-accept() { fzf-history-widget zle accept-line } zle -N fzf-history-widget-accept bindkey '^X^R' fzf-history-widget-accept
在命令行上按下alt-c, 会列出当前文件夹下的目录,选中条目会自动进入到相应目录
默承认以经过**来触发文件或目录的自动完成
COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TBA>
好比
vim **<TAB> vim ../mult**<TAB> cd ~/github/fzf**<TBA>
若是使用--preview选项, fzf会自动用外部程序打开如今条目的文件, {}会被fzf选中行内容代替
fzf --preview 'cat {}'
建议安装rougify(先安装ruby, 而后gem intall rouge
)
而后在.zshrc里用函数或别名
fzfp() { fzf --preview '[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (rougify {} || highlight -O ansi -l {} || coderay {} || cat {}) 2> /dev/null | head -500' alias tt='fzf --preview '"'"'[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (rougify {} || highlight -O ansi -l {} || coderay {} || cat {}) 2> /dev/null | head -500'"'"
函数是更好的方式, 用alias的话,为了绕开'的问题,须要用一个双引号加一个间引号再加一个双引号才能生成一个单引号
上图左侧是文件列表,右侧是rougify生成的预览窗口,能够用鼠标上下滚动,遗憾的是用键盘无法移动光标到右侧窗口进行上下滚动。
安装
wget https://github.com/changyuheng/zsh-interactive-cd/blob/master/zsh-interactive-cd.plugin.zsh cp zsh-interactive-cd.plugin.zsh ~/.fzf/shell echo 'source ~/.fzf/shell/zsh-interactive-cd.plugin.zsh' >> ~/.zshrc
cd后按ctrl-i就会打开fzf finder窗口
# fasd & fzf change directory - jump using `fasd` if given argument, filter output of `fasd` using `fzf` else z() { [ $# -gt 0 ] && fasd_cd -d "$*" && return local dir dir="$(fasd -Rdl "$1" | fzf -1 -0 --no-sort +m)" && cd "${dir}" || return 1 }
# fd - cd to selected directory fd() { local dir dir=$(find ${1:-.} -path '*/\.*' -prune \ -o -type d -print 2> /dev/null | fzf +m) && cd "$dir" }
# fda - including hidden directories fda() { local dir dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir" }
# fdr - cd to selected parent directory fdr() { local declare dirs=() get_parent_dirs() { if [[ -d "${1}" ]]; then dirs+=("$1"); else return; fi if [[ "${1}" == '/' ]]; then for _dir in "${dirs[@]}"; do echo $_dir; done else get_parent_dirs $(dirname "$1") fi } local DIR=$(get_parent_dirs $(realpath "${1:-$PWD}") | fzf-tmux --tac) cd "$DIR" }
# cf - fuzzy cd from anywhere # ex: cf word1 word2 ... (even part of a file name) # zsh autoload function cf() { local file file="$(locate -Ai -0 $@ | grep -z -vE '~$' | fzf --read0 -0 -1)" if [[ -n $file ]] then if [[ -d $file ]] then cd -- $file else cd -- ${file:h} fi fi }
# fasd & fzf change directory - open best matched file using `fasd` if given argument, filter output of `fasd` using `fzf` else v() { [ $# -gt 0 ] && fasd -f -e ${EDITOR} "$*" && return local file file="$(fasd -Rfl "$1" | fzf -1 -0 --no-sort +m)" && vi "${file}" || return 1 }
在.vimrc里用vunble安装
set rtp+=/home/harriszh/.fzf/ ... Plugin 'junegunn/fzf.vim' ...
而后FZF等命令就可使用了
建议要安装ag,并把FZF_DEFAULT_COMMAND改为ag
若是对FZF和vim和结合感兴趣能够看: VIM与模糊搜索神器FZF的集成用法 - 从简单到高级
若是对FZF和各类工具的配合使用请看: 模糊搜索神器FZF番外篇
fzf是很是强大的胶水工具,利用它和ag, fasd及shell command能够实现很是绚烂的功能。更多例子见wiki
若是上文有错误的地方,欢迎联系做者