Kubectl命令提示自动补全

更多可以参考Kubernetes中kubectl的文档:https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

启用 shell 自动补全功能

kubectl已经包含了自动补全的支持,kubectl可以自动生产补全脚本,因此只需要将其加入profile即可

常见启用方式如下,更多功能可以使用 kubectl completion -h 查看.

On Linux, using bash

在CentOS上, 如果没有安装过需要先按照bash-completion.

yum install bash-completion -y

然后执行以下命令即可临时启用自动补全功能,但是退出当前shell后会失效 

source <(kubectl completion bash).

运行下面的命令添加自动补全脚本到配置文件中,在启动时自动加载

echo "source <(kubectl completion bash)" >> ~/.bashrc

On macOS, using bash

On macOS, you will need to install bash-completion support via Homebrew first:

## If running Bash 3.2 included with macOS
brew install bash-completion
## or, if running Bash 4.1+
brew install bash-completion@2

Follow the “caveats” section of brew’s output to add the appropriate bash completion path to your local .bashrc.

If you installed kubectl using the Homebrew instructions then kubectl completion should start working immediately.

If you have installed kubectl manually, you need to add kubectl autocompletion to the bash-completion:

kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

The Homebrew project is independent from Kubernetes, so the bash-completion packages are not guaranteed to work.

Using Zsh

If you are using zsh edit the ~/.zshrc file and add the following code to enable kubectl autocompletion:

if [ $commands[kubectl] ]; then   
source <(kubectl completion zsh)
fi

Or when using Oh-My-Zsh, edit the ~/.zshrc file and update the plugins= line to include the kubectl plugin.

plugins=(kubectl)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据