文章

终端设置_tab补全

法1.安装bash-completion

1
2
3
安装完成之后,根据提示
在 ~/.bash_profile中添加此行
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

法2.新增文件.bashrc 在里面最后一行添加

1
2
3
4
complete -W "$(echo $(grep '^host ' ~/.ssh/config  | sort -u | sed 's/^ssh //'))" ssh
complete -W "$(echo $(grep '^host ' ~/.ssh/config  | sort -u | sed 's/^scp //'))" scp

source .bashrc

k8s

1
2
3
安装 bash-completion 完成之后
source /usr/share/bash-completion/bash_completion
echo 'source <(kubectl completion bash)' >> ~/.bashrc

docker

1
2
3
安装 bash-completion 完成之后
source /usr/share/bash-completion/bash_completion
source /usr/share/bash-completion/completions/docker

git

1
2
3
linux 
安装 bash-completion 完成之后
source /etc/bash_completion.d/git

mac git补全

1
2
3
4
5
6
7
8
9
10
11
12
打开网页,https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
全选,复制内容

粘贴到文件:  ~/.git-completion.bash
赋权: chmod u+x ~/.git-completion.bash

vim ~/.bash_profile
if  \[ -f ~/.git-completion.bash \]; then
    . ~/.git-completion.bash   
fi

source ~/.bash_profile

自定义补全配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
主要参数:

#======================================================================
1.compgen: 用于生成自动补全的候选项,可以用来生成文件、命令、变量等的补全项。

compgen [options] [word]

-c:生成所有可执行命令的候选项(包括命令路径);
> compgen -c  # 输出系统上所有可执行的命令
> compgen -c g   # 列出所有以字母 g 开头的命令

-f:生成文件和目录的候选项
> compgen -f /etc/   # 输出 /etc/ 目录下的所有文件和目录
> compgen -f /home/user/   # 列出 /home/user/ 下的所有文件和目录

-v:生成所有环境变量的候选项
> compgen -v    # 输出所有环境变量

-a:生成所有的别名
> compgen -a   # 输出所有定义的 Bash 别名


-A:基于指定类型生成补全项,A 后面可以指定补全类型(如 alias、function 等)
> compgen -A alias  # 输出所有别名。


#======================================================================
2.complete: 用来定制补全规则。用来定义特定命令的补全规则。它允许你指定一个命令,并为该命令设置自定义的补全行为

complete [options] [command]

-F:指定一个函数,Bash 会调用该函数来生成补全项
-F _mycommand_completions mycommand
_mycommand_completions 是自定义的函数,处理如何补全 mycommand


-W:给定一个单词列表,自动将这些单词作为补全候选项;
    由空格分隔的单词列表,表示补全的候选项

> complete -W "start stop restart" mycommand # 输入 mycommand 双击tab会补出start、stop、restart



例子: 
#======================================================================
使用 kw 补全 k8s config 配置文件

1./etc/bash_completion.d/kw.bash
#===============================
_kw()
{
    congfig_flag="$(ls ~/.kube/config_* | awk -F"config_" '{print $NF}')"
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $( compgen -W "${congfig_flag}" -- $cur ) )
}

complete -F _kw kw


2.~/.bashrc
#===============================
function kw(){
    local flag=$1
    if [ -z $flag ]; then
        flag="cnbj6_dev"
    fi
    config_file="$HOME/.kube/config_${flag}"
    if [ ! -f ${config_file} ]; then
        logger 1 "kubeconfig file [${config_file}] is not exist, quit!!!"
	return 1
    fi
    # 获取AWS 的Key
    def_key_id=$(     cat ~/.aws/credentials_hantu | grep aws_access_key_id     | awk -F"=" '{print $2}' )
    def_key_secret=$( cat ~/.aws/credentials_hantu | grep aws_secret_access_key | awk -F"=" '{print $2}' )
    chuhai_key_id=$(     cat ~/.aws/credentials_chuhai | grep aws_access_key_id     | awk -F"=" '{print $2}' )
    chuhai_key_secret=$( cat ~/.aws/credentials_chuhai | grep aws_secret_access_key | awk -F"=" '{print $2}' )

    export KUBECONFIG="${config_file}"
    if [[ "$flag" =~ "chuhai" ]] && [[ "$flag" != "config_ru_chuhai" ]]; then
        export AWS_ACCESS_KEY_ID="${chuhai_key_id}"
        export AWS_SECRET_ACCESS_KEY="${chuhai_key_secret}"
    else
        export AWS_ACCESS_KEY_ID="${def_key_id}"
        export AWS_SECRET_ACCESS_KEY="${def_key_secret}"
    fi
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\][\u@\h\[\033[00m\]: \[\033[01;34m\]\w\[\033[00m\]] (k8s集群[\[\033[01;31m\]'${flag}'\[\033[00m\]])\n\$ '
}

**注意: **

自定义补全,在mac电脑中 直接写入可能不生效,因为 MAC电脑 使用的是 brew安装的 bash-completion,

他安装软件的目录在: /opt/homebrew/Cellar/

而生效的目录是在: /opt/homebrew/etc

需要在此目录: /opt/homebrew/etc/bash_completion.d/ 创建一个软连接,如图

Image

本文由作者按照 CC BY 4.0 进行授权