V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Yggdroot
V2EX  ›  Vim

vim 的模糊查找插件 LeaderF 新功能介绍

  •  
  •   Yggdroot · 2018-12-30 17:36:39 +08:00 · 8154 次点击
    这是一个创建于 1936 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    上一篇文章《让人相见恨晚的 vim 插件:模糊查找神器 LeaderF》概括性的介绍了LeaderF的基本功能,本篇对 LeaderF 的新功能做一下介绍。

    统一的命令接口

    随着 LeaderF 功能的不断增多,LeaderF 的命令也在不断的增加,给开发和使用都带来一些不便。于是,LeaderF 提供了一个统一的命令接口,就像 shell 命令一样,不同的命令选项组合,带来不同的功能。
    可以通过:Leaderf -h来查看具体使用方法。

    usage: Leaderf[!] [-h] [--reverse] [--stayOpen] [--input <INPUT> | --cword]
                      [--top | --bottom | --left | --right | --belowright | --aboveleft | --fullScreen]
                      [--nameOnly | --fullPath | --fuzzy | --regexMode] [--nowrap]
                      {file,tag,function,mru,searchHistory,cmdHistory,help,line,colorscheme,self,bufTag,buffer,rg}
                      ...
    
    optional arguments:
      -h, --help            show this help message and exit
      --reverse             show results in bottom-up order
      --stayOpen            don't quit LeaderF after accepting an entry
      --input <INPUT>       specifies INPUT as the pattern inputted in advance
      --cword               current word under cursor is inputted in advance
      --top                 the LeaderF window is at the top of the screen
      --bottom              the LeaderF window is at the bottom of the screen
      --left                the LeaderF window is at the left of the screen
      --right               the LeaderF window is at the right of the screen
      --belowright          the LeaderF window is at the belowright of the screen
      --aboveleft           the LeaderF window is at the aboveleft of the screen
      --fullScreen          the LeaderF window takes up the full screen
      --nameOnly            LeaderF is in NameOnly mode by default
      --fullPath            LeaderF is in FullPath mode by default
      --fuzzy               LeaderF is in Fuzzy mode by default
      --regexMode           LeaderF is in Regex mode by default
      --nowrap              long lines in the LeaderF window won't wrap
    
    subcommands:
    
      {file,tag,function,mru,searchHistory,cmdHistory,help,line,colorscheme,self,bufTag,buffer,rg}
        file                search files
        tag                 navigate tags using the tags file
        function            navigate functions or methods in the buffer
        mru                 search most recently used files
        searchHistory       execute the search command in the history
        cmdHistory          execute the command in the history
        help                navigate the help tags
        line                search a line in the buffer
        colorscheme         switch between colorschemes
        self                execute the commands of itself
        bufTag              navigate tags in the buffer
        buffer              search buffers
        rg                  grep using rg
    
    If [!] is given, enter normal mode directly.
    

    这样就可以用:Leaderf <subcommand> [options]来执行 LeaderF 的命令了。例如:

    | Before         |     | Now  
    | ------         | :-: | ---
    | :LeaderfFile   | <=> | :Leaderf file
    | :LeaderfBuffer | <=> | :Leaderf buffer
    | :LeaderfMru    | <=> | :Leaderf mru
    | :LeaderfMruCwd | <=> | :Leaderf mru --cwd
    | ...            |     |   
    

    每个子命令都有专有的命令选项,可以用:Leaderf <subcommand> -h来查看。例如,Leaderf mru -h:

    usage: Leaderf[!] mru [-h] [--cwd] [--no-split-path] [--reverse] [--stayOpen]
                          [--input <INPUT> | --cword]
                          [--top | --bottom | --left | --right | --belowright | --aboveleft | --fullScreen]
                          [--nameOnly | --fullPath | --fuzzy | --regexMode]
                          [--nowrap]
    
    optional arguments:
      -h, --help       show this help message and exit
    
    specific arguments:
      --cwd            search MRU in current working directory
      --no-split-path  do not split the path
    
    common arguments:
      --reverse        show results in bottom-up order
      --stayOpen       don't quit LeaderF after accepting an entry
      --input <INPUT>  specifies INPUT as the pattern inputted in advance
      --cword          current word under cursor is inputted in advance
      --top            the LeaderF window is at the top of the screen
      --bottom         the LeaderF window is at the bottom of the screen
      --left           the LeaderF window is at the left of the screen
      --right          the LeaderF window is at the right of the screen
      --belowright     the LeaderF window is at the belowright of the screen
      --aboveleft      the LeaderF window is at the aboveleft of the screen
      --fullScreen     the LeaderF window takes up the full screen
      --nameOnly       LeaderF is in NameOnly mode by default
      --fullPath       LeaderF is in FullPath mode by default
      --fuzzy          LeaderF is in Fuzzy mode by default
      --regexMode      LeaderF is in Regex mode by default
      --nowrap         long lines in the LeaderF window won't wrap
    
    If [!] is given, enter normal mode directly.
    

    自下而上显示结果

    好多从 ctrlp.vim 转过来的朋友说不习惯 LeaderF 自上而下显示结果的方式,想要 LeaderF 也能像 ctrlp 一样自下而上(最优结果在最下面)显示结果,为了回馈各位朋友对 LeaderF 的支持,现在 LeaderF 也添加了自下而上显示搜索结果的功能: LeaderF_reverse_order

    只需要在命令后面加上--reverse即可,或者也可以一劳永逸,在 vimrc 里面加上 let g:Lf_ReverseOrder = 1

    And 模式

    LeaderF 支持用空格(可以用g:Lf_AndDelimiter来修改)作为 And 操作符来进行模糊匹配。当匹配已经匹配到字符串末尾时,可以通过敲空格和一个子串来匹配字符串的前面部分,进行快速过滤。
    NOTE: 空格分隔的每个子串都是对整个字符串的模糊匹配,而不是精确匹配。

    And 模式

    模糊匹配历史

    可以通过上下键来翻看模糊匹配历史: 翻看历史

    总结

    LeaderF 精益求精,永无止境。

    17 条回复    2020-04-28 21:29:27 +08:00
    Sparetire
        1
    Sparetire  
       2018-12-30 17:50:26 +08:00
    glues
        2
    glues  
       2018-12-30 17:50:42 +08:00 via iPhone
    hanxiV2EX
        3
    hanxiV2EX  
       2018-12-30 21:29:16 +08:00 via Android
    精品!
    dongqihong
        4
    dongqihong  
       2018-12-30 21:50:14 +08:00
    超级赞!已经安利了很多同事在用,因为工作原因必须在服务器用 vim 开发,LeaderF 在大型项目中使用效果很出色,速度贼快,大概 100w 行代码的目录中,搜起来很爽!
    感谢开发者!!!
    jdhao
        5
    jdhao  
       2018-12-30 22:40:22 +08:00
    赞,在用楼主的 indentLine 插件,没想到楼主是国人。。。
    palytoxin
        6
    palytoxin  
       2018-12-31 12:40:03 +08:00 via iPhone
    速度很快,之前 ctrlp 为了速度还装了 fd,不过还是不如 leaderf
    Yggdroot
        7
    Yggdroot  
    OP
       2018-12-31 13:26:18 +08:00
    @dongqihong 多谢安利。
    Chingim
        8
    Chingim  
       2018-12-31 19:26:19 +08:00 via Android
    不知道跟 fzf 相比优劣势如何
    heikejia
        9
    heikejia  
       2019-01-01 20:31:50 +08:00
    @Chingim 兼容所有操作系统,fzf 不一定,也许可以搞定,但是麻烦
    Yggdroot
        10
    Yggdroot  
    OP
       2019-01-01 21:11:20 +08:00
    @Chingim https://www.zhihu.com/question/23590572/answer/546352496
    这个下面有个小伙伴的对比:虽然不一定准确,也有参考价值。

    > 异步 Fuzzy Finder:vim 的 fuzzy finder 可不是只能像命令行里的 fzf 那样简单地 fuzzy
    > match,下面推荐的几个 fuzzy
    > finder 插件你可以用它来:搜索字符串,对 grep 结果进行过滤(不需要其它 grep 插件了),浏览和跳转 tags(不需要 tagbar 了),浏览、跳转以及管理 buffer、files,跳转最近使用的文件(MRU),搜索历史剪切板内容,切换 color
    > scheme,甚至浏览你的 github 星标版本库。
    > 1. denite.nvim 最高的可定制度,最高的可扩展性,最多的 source,也最不好上手。依赖 python。
    >
    > 2. fzf.vim 开箱即用,新手友好,无需太多配置,source 也足够多。但它实际上 fzf 的一个 vim 前端,并不是一个典型的 vim 插件,你需要安装 fzf 才能使用,不过也正是因为如此,它用起来非常丝滑。另外它缺乏官方文档,可定制度也较差。不依赖 python。
    >
    > 3. LeaderF 开箱即用,新手友好,无需太多配置。它的算法非常厉害(个人觉得只有 fzy 能和它比),另外它也提供了很多非常实用的选项(比如正则过滤),但是 source 相比前两者较少。依赖 python。
    >
    > 2018.12.20 最近的一个 32 万行代码的模糊匹配测试中,对下面几个模糊匹配工具进行了测试:leaderf, fzf, skim, ctrlp 的默认 matcher, denite 的默认 matcher,cpsm ( denite 和 ctrlp 的 matcher 之一),
    > fruzzy ( denite 和 ctrlp 的 matcher 之一)按匹配精准度和匹配速度来衡量它们的性能的话,leaderf 的表现是最好的。
    > Note: leaderf 的关键部分是用 c 写的
    >
    > 另外,关于 ctrlp。ctrlp 已经有了异步模式,但是它的性能和 leaderf,
    > fzf 不在一个档次,并且群里有大佬说它的异步还有很多 bug。如果你的 vim 没有 python 支持,个人推荐用 fzf
    yuuko
        11
    yuuko  
       2019-01-11 18:04:43 +08:00
    今天用了一下,相同的项目文件检索速度比 denite 快很多,很棒
    y_ding
        12
    y_ding  
       2019-01-30 16:30:05 +08:00
    好东西!!赞!!
    tamlok
        13
    tamlok  
       2019-02-02 07:10:29 +08:00 via Android
    还是希望能在 leaderf 的模式中支持 vim 命令行的一些快捷键,比如`ctrl-r ctrl-w`输入当前光标所在单词,`ctrl-r`读取特定寄存器等。
    Yggdroot
        14
    Yggdroot  
    OP
       2019-02-02 09:26:55 +08:00 via Android
    @tamlok 不知道你的应用场景是怎样的?用 Leaderf 一般只要输入几个字符就可以找到目标,想搜光标下的单词,有个'--cword'选项,也支持 ctrl-v 粘贴,读寄存器来做什么呢
    tamlok
        15
    tamlok  
       2019-02-02 09:37:43 +08:00 via Android
    @Yggdroot 因为 vim 里面有 26 个字母寄存器,可以存放不同的内容。ctrl-r ctrl-w 纯粹是 vim 里面的输入习惯,想输入当前词就按这个,肌肉记忆。
    zhuzhzh
        16
    zhuzhzh  
       2020-04-03 09:20:57 +08:00
    如何在几个匹配结果中来上下移动选择啊

    现在的上下键是显示之前搜索内容

    tab 也不能移动选中项
    Yggdroot
        17
    Yggdroot  
    OP
       2020-04-28 21:29:27 +08:00
    @zhuzhzh ctrl-j ctrl-k
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3268 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:48 · PVG 20:48 · LAX 05:48 · JFK 08:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.