之前在 「创建 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. JavaScriptBOM操作

    BOM(浏览器对象模型)主要用于管理浏览器窗口,它提供了大量独立的.可以与浏览器窗口进行互动的功能,这些功能与任何网页内容无关.浏览器窗口的window对象是BOM的顶层对象,其他对象都是该对象的子对 ...

  2. canal数据同步的环境配置

    canal数据同步的环境配置:(适用于mysql) 前提:在linux和windows系统的mysql数据库中创建相同结构的数据库和表,我的linux中mysql是用docker实现的(5.7版本), ...

  3. MySQL 常用命令手册 增删改查大法

    一.数据库操作 创建数据库 语法: CREATE DATABASE database_name; 删除数据库 删除数据库务必谨慎!因为执行删除命令后,所有数据将消失. 语法: DROP DATABAS ...

  4. 页面强制重新加载js的办法

    1:线上强制重新加载js的办法 js后缀?v1.0 2:开发环境强制重新加载js的办法?now=Date.now() 3:开发环境强制重新加载js的办法F12进入调试页面选择network下单 dis ...

  5. 微信小程序:Navigator导航组件

    导航组件:类似超链接标签. url:要跳转的页面路径,可以放绝对路径,也可以放相对路径,绝对路径指从pages作为根目录开始找到你要的页面. 找到你要找的页面的相对地址的方法:在vscode中,该页面 ...

  6. 第48天学习打卡(HTML 行内元素和块元素 列表 表格 视频和音频 页面结构分析 iframe内联框架 表单语法 )

    行内元素和块元素 块元素 ​ 无论内容多少,该元素独占一行 ​ (p.h1-h6) 行内元素 ​ 内容撑开宽度,左右都是行内元素的可以排在一行 ​ (a.strong.em...) 列表 什么是列表 ...

  7. LeetCode-133克隆图(图的遍历+深拷贝概念)

    克隆图 LeetCode-133 使用一个map来存储已经遍历的结点,这个存起来的结点必须是新new的才符合题意 /* // Definition for a Node. class Node { p ...

  8. double型数据的输入和输出--%f和%lf

    scanf函数是通过指针指向变量的. %f告诉scanf函数在所传地址位置上存储一个float型值, 而%lf告诉scanf函数在所传地址位置上存储一个double型值. 这里float和double ...

  9. Java程序员必备后台前端框架--Layui【从入门到实战】(一)

    layui入门使用及图标的使用 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] [编程工具:IDEA] 下载Layui与文件分析 下载直接去官网下载即可 文件分析 下载完成后,解压会 ...

  10. 关于PHP的表单提交显示

      实现功能:html页面,向表单内填入账号.密码:php页面,将填写的账号与密码信息展示出来. demo6.html代码如下: 1 <!DOCTYPE html> 2 <html& ...