By bitmap
Hi all,
I am just getting started with Kubernetes and the kubectl
command specifically.
As there are so many flags and arguments, is there a way to enable autocompletion just like in git
?
So for example when I type kubectl get no[TAB]
it would autocomplete nodes
.
Thank you!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hi there,
Yes, this is doable. What you would need to do is to first to make sure that you have the bash-completion
package installed. If not, you could install it with the following command:
sudo apt-get install -y bash-completion
After that, you could generate the required kubectl
completion rules with the kubectl completion bash
command, and then store the rules in your .bashrc
file. You could 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:
source ~/.bashrc
Finally, if you were not now run kubectl get no[TAB]
it will be autocompleted.
This is indeed quite handy! Hope that this helps. Regards, Bobby
I would look at this page https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-autocomplete
source <(kubectl completion bash) # set up autocomplete in bash into the current shell, bash-completion package should be installed first.
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell
You could use it for aliases as I usually use k
instead of the full kubectl
command it is usefull
alias k=kubectl
complete -o default -F __start_kubectl k
(kubectl completion zsh) # set up autocomplete in zsh into the current shell
echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc # add autocomplete permanently to your zsh shell
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.