之前在 「创建 fish shell 自动补全文件」 中介绍了如何创建 fish 的补全文件,实现对命令的友好补全提示。通过形如 complete -c <command> -a ["参数列表"] 的脚本来实现的。

比如 complete -c myprog -a "yes no" 可在输入 myprog 后通过 TAB 唤起提示:

$ myprog<tab>
no yes

但如果 <comamnd> 包含子命令时,则需要麻烦些。比如拿 git commit 来说,为了实现在输入该命令时提示一些信息,实测如下形式是不行的:

complete -c git-commit -a "yes no"
complete -c "git commit" -a "yes no"

同时,想要实现 git commit -m<TAB> 在进行 git 提交时,进行一些提示,除了需要解决上面子命令判定的问题,还需要判定这里的 -m 参数。

判断子命令

查看 fish 在 GitHub 的仓库可发现,在 fish-shell/share/functions/ 目录下提供了很多工具方法, 通过 __fish_seen_subcommand_from 配合 complete-n 参数可实现子命令的判定。

该工具方法需要结合 -n(condition)参数,指定一段 shell 脚本,当脚本返回 0 时所指定的自动补全才生效。

complete -f -c git -n '__fish_seen_subcommand_from commit' -a 'test' -d "the test command will appear after commit"

上述脚本实现的效果,是在输入 git commit 之后,TAB 会触发 test 命令的自动补全,当且仅当 git 后跟的是 commit 这个子命令。

-s -l 的用法

一般命令会需要带参数,这些参数或是通过 - 加缩写或 -- 加完整的参数名称提供。通过 -s(short)、 -l(long) 便可指定命令需要补全的参数名称。

complete -f -c git -n '__fish_seen_subcommand_from commit' -s m --l message -d "specify the commit message"
complete -f -c git -n '__fish_seen_subcommand_from commit' -s f --l foo -d "argument foo"
complete -f -c git -n '__fish_seen_subcommand_from commit' -s b --l bar -d "argument bar"
complete -f -c git -n '__fish_seen_subcommand_from commit' -s b --l baz -d "argument baz"
complete -f -c git -n '__fish_seen_subcommand_from commit' -s b --l quz -d "argument quz"

上述脚本实现的效果是,在输入 git commit -<TAB> 后,参数列表会作为候选补全列出来:

$ git commit -
-b --quz (argument quz) -m --message (specify the commit message) --baz (argument baz)
-f --foo (argument foo) --bar (argument bar)

可以看到,默认情况下,这些参数按字母顺序进行了重排,可通过 -k (keep order) 来保持书写时指定的顺序。-k 参数对于 -a 指定的命令也适用。

判定参数

通过 __fish_seen_argument 工具方法,可判定输入的命令后有没有跟指定参数。

complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'build' -d "Changes that affect the build system or \r\ntexternal dependencies (example scopes: gulp, broccoli, npm)"

这里判定参数的写法,和前面 -s -l 中介绍的一致。

上述脚本实现的效果是,当输入 git commit -m<TAB> 时会自动补上 build 命令。

到这里就完成了子命令和参数的判定。接下来,就可以用于一些实用的场景,比如 Angular 的提交消息规定了如下的形式:

<type>(<scope>): <short summary>

其中 <type> 包含如下可选值:

  • build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
  • ci: Changes to our CI configuration files and scripts (example scopes: Circle, BrowserStack, SauceLabs)
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • test: Adding missing tests or correcting existing tests

可通过前面介绍的方法,将这里的 type 在进行 git commit 时给提示出来。

实现 Angular commit type 的自动提示

最后,实现这一效果的脚本大概是这样子:

complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'build' -d "Changes that affect the build system or \r\ntexternal dependencies (example scopes: gulp, broccoli, npm)"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'ci' -d "Changes to our CI configuration files and scripts (example scopes: Circle, BrowserStack, SauceLabs)"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'docs' -d "Documentation only changes"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'feat' -d "A new feature"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'fix' -d "A bug fix"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'perf' -d "A code change that improves performance"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'refactor' -d "A code change that neither fixes a bug nor adds a feature"
complete -k -f -c git -n '__fish_seen_subcommand_from commit; and __fish_seen_argument -s m -l message' -a 'test' -d "Adding missing tests or correcting existing tests"

相关仓库 wayou/angular-commit-complete

相关资源

The text was updated successfully, but these errors were encountered:

fish shell 自动补全子命令的更多相关文章

  1. shell自动补全功能:bash和zsh

    首要一点:shell有多种,比如bash.zsh.csh.ksh.sh.tcsh等 因此,制作自动补全功能时,要先搞清楚,你使用的是哪种shell,各个shell制作方法是不同的,网上大部分介绍的是关 ...

  2. zsh 自动补全导致命令显示重复

    关键字:autocomplete, zsh, backspace, securecrt, xterm, linux console 举个例子: 输入命令ls  然后按TAB补全试试,发现竟然是这样的 ...

  3. python命令行添加自动补全和命令历史功能

    # python startup file import readline import rlcompleter import atexit import os # tab completion re ...

  4. Linux python <tab>自动补全

    为Python添加交互模式下TAB自动补全以及命令历史功能. 1.获取python目录 [root@localhost ~]# python Python 2.6.6 (r266:84292, Jul ...

  5. Linux实战(12):解决Centos7 docker 无法自动补全

    环境:centos最小化安装,会出现一些命令无法自动补全的情况,例如在docker start 无法自动补全 start 命令,无法自动补全docker容器名字.出现这种情况的可参考以下操作: yum ...

  6. ipython, 一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数

    一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数. 若用的是fish s ...

  7. 使用Linux自定义自动补全命令完善自己的shell脚本

    对于Linuxer来说,自动补全是再熟悉不过的一个功能了.当你在命令行敲下部分的命令时,肯定会本能地按下Tab键补全完整的命令,当然除了命令补全之外,还有文件名补全. Bash-completion ...

  8. Docker 命令自动补全?要的

    前言 不知道这个小伙伴有多久没用过 Docker 了, 突然对我说 Docker 命令怎么发生变化了 docker run ... #变成了 docker container run ... 他说,本 ...

  9. [Git]08 如何自动补全命令

     [Git]08如何自动补全命令 如果你用的是 Bash shell,可以试试看 Git 提供的自动完成脚本.下载 Git 的源代码,进入 contrib/completion 目录,会看到一个g ...

随机推荐

  1. scala:函数作为值或参数进行传递、作为返回值进行返回

    @ 目录 函数可以作为值进行传递 函数可以作为参数进行传递 函数可以作为返回值进行返回 什么是匿名函数 函数可以作为值进行传递 语法var f = 函数名 _ 如果明确了变量的数据类型,那么下划线可以 ...

  2. 这一篇TCP总结请收下

    前言 很高兴遇见你~ TCP这些东西,基本每个程序猿都或多或少是掌握的了.虽然感觉在实际开发中没有什么用武之处,但,面试他要问啊 而最近大家伙过完年,也都在准备春招,我也一样.阅读了一些okHttp源 ...

  3. CMD(命令提示符)的基本操作(文件夹)

    打开CMD窗口,接下来将介绍如何使用CMD来创建.删除.修改.查看文件夹(目录) ps:以下所有文件夹将统一写成目录 1.1 使用CMD创建空目录(为了更好的演示,本文皆以D盘为当前路径),命令如下: ...

  4. Pycharm模块导入失败,带有红色波浪线。

    在Pycharm中打开一个python开源工程,结果在导入库的部分一堆红色波浪线显示错误,并且按住Ctrl + 鼠标左击无法跳转到指定类或方法,如下图所示. 解决方法: (1)首先忽略掉这些报错,先运 ...

  5. XAPKInstaller - XAPK游戏包安装器

    XAPKInstaller 一个用于安装XAPK游戏包的安装器. 程序需要读写存储与获取已安装应用权限才可正常运行. 长按条目可显示文件的详细信息. SDK小于24(Android N)的设备会显示应 ...

  6. POJ-1087(最大流+EK算法)

    A Plug for UNIX POJ-1087 这一题也是最大流的应用,但是题目有点绕. 题目的意思就是有插座和插头,只不过这里使用设备取代了插头.只有插座和设备进行匹配了. 题目要注意的是那个适配 ...

  7. 文件包含-phpinfo()

    我也是听朋友说有这个姿势的,github有集成好的环境 先讲一下利用phpinfo上传文件,然后在文件包含的原理: 在给PHP发送POST数据包时,如果数据包里包含文件区块,无论访问的代码中是否有处理 ...

  8. 七种方案!探讨Redis分布式锁的正确使用姿势

    前言 日常开发中,秒杀下单.抢红包等等业务场景,都需要用到分布式锁.而Redis非常适合作为分布式锁使用.本文将分七个方案展开,跟大家探讨Redis分布式锁的正确使用方式.如果有不正确的地方,欢迎大家 ...

  9. dubbo实战之四:管理控制台dubbo-admin

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  10. WPF 应用 - 通过 js 缩放 System.Windows.Controls.WebBrowser 的内容

    1. 前提 原本是在大屏上展示系统,系统有个功能是加载第三方的网站,第三方网站按照大屏的分辨率写死了宽高: 现需要改到小屏展示系统,而这个第三方的网站不能随着 WebBrowser 窗口的尺寸调整网站 ...