kubectl Shell Autocomplete — #HeptioProTip

Ross Kukulinski
Heptio
Published in
2 min readJun 15, 2017

Did you know that kubectl provides shell autocompletion for BASH and ZSH? Shell command-line completion allows you to quickly build your command without having to type every character. The Kubernetes documentation provides great instructions on how to set it up for your development environment.

Here’s how kubectl completion works in Bash.

First, get a list of all `kubectl` commands:

$ kubectl TAB TAB
annotate autoscale convert describe expose patch rollout top api-versions certificate cordon drain get port-forward run uncordon apply cluster-info cp edit label proxy scale version attach completion create exec logs replace set auth config delete explain options rolling-update taint

We can then start typing `g` and then press TAB which will then autocomplete `get`.

$ kubectl g TAB
$ kubectl get
$ kubectl get de TAB
$ kubectl get deployment

Now press ENTER to run the command.

That’s all fabulous, but check this next-level #HeptioProTip: `kubectl` autocomplete can also pull information from your Kubernetes cluster using your currently defined context. Check it out:

Try pressing TAB TAB instead of ENTER at the end of the previous command:

$ kubectl get deployment TAB TAB
kube-dns kube-dns-autoscaler kube-cert-manager heapster-v1.3.0

Now try:

$ kubectl get deployment kube-d TAB
$ kubectl get deployment kube-dns

Hope you enjoyed this #HeptioProTip! Follow @heptio for more tips and tricks.

Sign up to discover human stories that deepen your understanding of the world.