mac linux自动补全,shell自动补全功能:bash和zsh

首要一点: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 asthe 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 cursoris at the end of the line, its character index isthe 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 isgiven, 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值