concept tab completion in category kubernetes

appears as: tab completion
Kubernetes in Action, Second Edition

This is an excerpt from Manning's book Kubernetes in Action, Second Edition.

Configuring tab completion for kubectl

Even with a short alias like k, you’ll still have to type a lot. Fortunately, the kubectl command can also output shell completion code for both the bash and the zsh shell. It enables tab completion of not only command names but also the object names. For example, later you’ll learn how to view details of a particular cluster node by executing the following command:

$ kubectl describe node gke-kubia-default-pool-9bba9b18-4glf

That’s a lot of typing that you’ll repeat all the time. With tab completion, things are much easier. You just press TAB after typing the first few characters of each token:

$ kubectl desc<TAB> no<TAB> gke-ku<TAB>

To enable tab completion in bash, you must first install a package called bash-completion and then run the following command (you can also add it to ~/.bashrc or equivalent):

Kubernetes in Action

This is an excerpt from Manning's book Kubernetes in Action.

Configuring tab completion for kubectl

Even with a short alias such as k, you’ll still need to type way more than you’d like. Luckily, the kubectl command can also output shell completion code for both the bash and zsh shell. It doesn’t enable tab completion of only command names, but also of the actual object names. For example, instead of having to write the whole node name in the previous example, all you’d need to type is

$ kubectl desc<TAB> no<TAB> gke-ku<TAB>


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":39}]]"}
!@%STYLE%@!

To enable tab completion in bash, you’ll first need to install a package called bash-completion and then run the following command (you’ll probably also want to add it to ~/.bashrc or equivalent):

$ source <(kubectl completion bash)


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":35}]]"}
!@%STYLE%@!

But there’s one caveat. When you run the preceding command, tab completion will only work when you use the full kubectl name (it won’t work when you use the k alias). To fix this, you need to transform the output of the kubectl completion command a bit: