命令行上的narrowing(随着输入逐步减少备选项)工具

前面在介绍zsh的时候,说过它的补全用来起比bash的Tab补全方便多了,在有多个备选项是你只要用光标键来挑选就是了,而不是全列出来提示你再多输入几个字符。而Emacs的anything / helm 则更近一步,首先就把备选项列出来,随着你的输入减少备选项,少到一定程度你就可以用眼睛浏览并进行选择了。

说起来有点费劲,看一下peco的这个演示图就明白了:

peco demo

最近看到几个命令行上的这种工具,可以让我们在bash或者vim里面也用上这种机制(至于具体用来干什么,就看自己的小宇宙了)

| name | homepage | language | github stars | pros | cons | comment < | | |
|------------+-------------------------------------------+----------+--------------+-----------------------------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---+---|
| fzf | https://github.com/junegunn/fzf | go/ruby | 3696 | vim, tmux, bash/zsh support | | A command-line fuzzy finder written in Go \n旧版本采用ruby编写,安装时对于没有提供二进制包的平台,会安装这个ruby版本的 | | |
| peco | https://github.com/peco/peco | go | 2793 | arm/windows binary | | 设计上尽力模仿了percol | | |
| PathPicker | https://github.com/facebook/PathPicker | python | 2718 | | | PathPicker accepts a wide range of input -- output from git commands, grep results, searches -- | | |
| percol | https://github.com/mooz/percol | python | 1938 | 拼音 | | adds flavor of interactive filtering to the traditional pipe concept of UNIX shell | | |
| selecta | https://github.com/garybernhardt/selecta/ | ruby | 1000 | vim | | A fuzzy text selector for files and anything else you need to select. Use it from vim, from the command line, or anywhere you can run a shell command. | | |
| pick | https://github.com/thoughtbot/pick | c | 349 | vim; 已经进入Debian 9 | | https://robots.thoughtbot.com/announcing-pick | | |
| zsh-zaw | https://github.com/zsh-users/zaw | zsh | 289 | 很多预配置sources | | zsh anything.el-like widget. | | |
| canything | https://github.com/keiji0/canything | c | 23 | | | | | |

参见: fuzzy matchers overview? - commandline

我目前在用的是 fzfpercol/peco 。对于zsh-zaw也用过一阵,但总觉得有点过于机巧,并且还得需要zsh。

fzf

https://github.com/junegunn/fzf

之所以用它主要是因为它直接提供了几个使用场景(见下面的描述),而percol/selecta这些只是提供了一个“挑选”的机制(按Emacs的举例而言,就是percol这些只提供了 anything.el 而没有提供 anything-config.el )。另外,虽然fzf并没有为树莓派提供预编译的版本(我也还不知道怎么自己装golang环境来编译),但它还有一个ruby版本,使用起来差别不大。

fzf 预提供的两个功能:

• 自己输入 fzf /home/johndoe 来查找文件

select file with fzf

• 利用它与bash/zsh的集成,来补全命令参数。跟bash的集成方法是 source fzf-0.10/shell/completion.sh && source fzf-0.10/shell/key-binding.sh ,然后就可以使用了:输入vim **<tab> 它就会调fzf列出当前目录所有文件让你选,输入ssh **<tab>它就列出~/.ssh/config里面的所有主机给你选

另外fzf提供了一个跟tmux配合的脚本(fzf-tmux),用它来挑备选项的话,它会检测当前是否在tmux里面,如果是的话就用tmux临时开一个面板来让你跟它交互。

peco/percol

这两个的功能基本上一致,都是列出一排备选项供用户挑,至于拿挑出来的结果干什么,则完全是用户自己来想,比如 vim $(locate -b '*.org' | fzf)

peco基本上是安装percol的设计来实现的,区别是percol是python编写的,peco是用go,结果是后者只需要单个可执行文件即可工作,并且性能好得多。

percol/peco的功能其实fzf 也支持(比如 vim $(locate -b '*.org' | fzf) 这种场景,但在fzf之余还用percol/peco的原因是:

• fzf 对非ansi字符支持得不好,会乱码,而peco和percol都支持utf-8
• percol 支持按拼音首字母匹配(比如输入bj会匹配出带有“编辑”、“比较”、“悲剧”、“北京”、“背景”的行。需要先pip install pinyin然后用percol --match-method=pinyin。我贡献的补丁,不过commit message里面错把pinyin写成pinin了)。
• 如果备选项很多(比如我想搜索博客园闪存的离线文件,目前有4000行左右),ruby版本的fzf 和 python写的percol 有性能上的问题,那就用peco好了(peco还提供了一个armv7的预编译版本,可以在树莓派上用)

posted @ 2016-01-18 16:20  巴蛮子  阅读(2836)  评论(3编辑  收藏  举报