Question

How to Enable kubectl Autocompletion in Bash?

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!


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 20, 2021
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

Essentially for:

Bash

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

ZSH

(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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.