什麼是命令自動補全
在 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 <http>
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
閱讀更多 IT男劉加加 的文章