什么是命令自动补全
在 Linux 命令行中,当输入字符后,按Tab 键,Shell 就会列出以这些字符开头的所有可用命令,如果只有一个命令匹配到,按一次 Tab 键就自动将这个命令补全。
如果输入pass,此时按 Tab 键,因为以pass开头的命令只有passwd这个命令, Shell 就会自动补全 passwd 命令。
另外如果输入的字符匹配多个命令则会列出所有可用的命令,比如,如果输入do,此时按 Tab 键 Shell 就会自列出可用的命令。
[root@node2 ~]# do
do docker-containerd-shim dockerd done
docker docker-containerd-shim-current dockerd-current
docker-containerd docker-ctr-current docker-storage-setup
docker-containerd-current docker-current domainname
但是系统自带的命令补全功能有限,自动补全功能仅限于命令和文件名。
可以安装 Bash 命令补全增强软件包 bash-completion来实现更多命令的补全。
bash-completion的安装
以Centos为例
[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
直接通过yum命令安装
yum install bash-completion
bash-completion版本信息
[root@node1 completions]# rpm -qi bash-completion
Name : bash-completion
Epoch : 1
Version : 2.1
Release : 6.el7
Architecture: noarch
Install Date: Thu 19 Mar 2020 07:59:19 AM CST
Group : Unspecified
Size : 264934
License : GPLv2+
Signature : RSA/SHA256, Fri 04 Jul 2014 08:47:01 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM : bash-completion-2.1-6.el7.src.rpm
Build Date : Tue 10 Jun 2014 08:05:27 AM CST
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem
Vendor : CentOS
URL : http://bash-completion.alioth.debian.org/
Summary : Programmable completion for Bash
Description :
bash-completion is a collection of shell functions that take advantage
of the programmable completion feature of bash.
配置命令补全
source /usr/share/bash-completion/bash_completion
命令补全的脚本存放在/usr/share/bash-completion/completions/目录,可以在这个目录查看支持增强补全功能的命令。
再试试输入docker命令后按tab键:
[root@node1 completions]# docker
build diff history inspect logs port restart search stats top wait
commit events image kill network ps rm secret stop unpause
container exec images load node pull rmi service swarm update
cp export import login pause push run stack system version
create help info logout plugin rename save start tag volume
[root@node1 completions]# docker
可以看到后续可用命令的提示,妈妈再也不用担心我记不住命令了。
同样对于kubenetes也可以实现命令的自动补全和提示:
[root@node1 ~]# kubectl
annotate certificate cordon drain get port-forward run uncordon
api-versions cluster-info cp edit label proxy scale version
apply completion create exec logs replace set
attach config delete explain options rolling-update taint
autoscale convert describe expose patch rollout top
[root@node1 ~]# kubectl
git的命令补全
如果是在安装bash-completion之后安装git是不能自动命令补全,需要手动进行一些配置。
git安装之后文档里会有git-completion.bash文件
[root@node1 bash-completion]# find / -name git-completion*
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.bash
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.tcsh
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.zsh
/usr/share/bash-completion/completions/git-completion.bash
/usr/share/git-core/contrib/completion/git-completion.tcsh
[root@node1 bash-completion]#
安装完git之后重新source一下bash-completion
source /usr/share/bash-completion/bash_completion
看看配置之后的效果
[root@node3 ~]# git
add cherry difftool init reflog show
am cherry-pick fetch log relink show-branch
annotate clean filter-branch merge remote stage
apply clone format-patch mergetool repack stash
archive column fsck mv replace status
bisect commit gc name-rev request-pull submodule
blame config get-tar-commit-id notes reset subtree
branch credential grep pull revert tag
bundle describe help push rm whatchanged
checkout diff imap-send rebase shortlog
[root@node3 ~]# git
如果觉得有用记得收藏点赞