码迷,mamicode.com
首页 > 系统相关 > 详细

shell自动补全功能:bash和zsh

时间:2019-03-15 17:38:56      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:assign   命令   and   script   ide   mamicode   info   variable   usr   

首要一点:shell有多种,比如bash、zsh、csh、ksh、sh、tcsh等

因此,制作自动补全功能时,要先搞清楚,你使用的是哪种shell,各个shell制作方法是不同的,网上大部分介绍的是关于bash的。

 

一、bash:

涉及命令:

补全命令complete、筛选命令compgen、修改补全命令设置compopt

内置变量:

通过这些变量,可以获得当前命令的内容、位置等信息,以便判断下面应该出现的提示命令的内容

除了上面三个命令外,Bash还有几个内置变量来辅助补全功能,如下: 

 

variabledescription
COMP_WORDS 类型为数组,存放当前命令行中输入的所有单词
COMP_CWORD 类型为整数,当前输入的单词在COMP_WORDS中的索引
COMPREPLY 类型为数组,候选的补全结果
COMP_WORDBREAKS 类型为字符串,表示单词之间的分隔符
COMP_LINE 类型为字符串,表示当前的命令行输入字符
COMP_POINT 类型为整数,表示光标在当前命令行的哪个位置

 

帮助信息:

man complete

补全脚本位置(Mac):

每打开一个新的shell,都会把这个目录下的脚本执行一遍

/usr/local/etc/bash_completion.d

 

参考:

https://mp.weixin.qq.com/s/nSje0zhcP2vAmBAH05g64w

 

 

二、zsh:

涉及命令:compctl

内置变量:没有bash的那些内置的变量,但是有个内置read函数,可以获取当前出现指令集、当前的指令内容、前一个指令内容等信息。

 

帮助信息:

通过这条命令查找zsh的内置函数,找到read的用法:

man zshbuiltins

read函数所用参数解释:

-A     The first name is taken as the name of an array and all words are assigned to it.
-n     Together with -c, the number of the word the cursor is on is read.  With -l, the index of the character the cursor is on is read.  Note that the command name is word number 1, not word  0,  and
       that when the cursor is at the end of the line, its character index is the length of the line plus one.
-c
-l     These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl).  If the -c flag is given, the words of the current command are read. If the
       -l flag is given, the whole line is assigned as a scalar.  If both flags are present, -l is used and -c is ignored.

 

man zsh

补全脚本位置(Mac):每个新的shell终端都会把这个脚本执行一遍,所以内容太多,一是占用内存,而是新的terminal启动慢

~/.zshrc

 

三、brew search comple

这种模糊搜索可以查到很多工具的命令自动补全脚本:比如pip、bash、docker、zsh等

技术图片

 

shell自动补全功能:bash和zsh

标签:assign   命令   and   script   ide   mamicode   info   variable   usr   

原文地址:https://www.cnblogs.com/shengulong/p/10538252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!