Enable kubectl Autocompletion - Tanzu Community Edition
Updated: Jan 11, 2022
Make sure that you have the bash-completion package installed. If not, you can install it with the following command:
sudo apt-get install -y bash-completion
After that, generate the required kubectl completion rules with the kubectl completion bash command, and then store the rules in your .bashrc file. You can use the following command to do that:
echo "source <(kubectl completion bash)" >> ~/.bashrc
Note: Make sure to run this only once so that you don’t get duplicated rules.
After that, use the source command to activate the new rules:
Finally, if you now run kubectl get no[TAB] it will be autocompleted.
To make kubectl work for your non-root user, run these commands:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config