Kubectl Autocomplete

Recently, we migrated our development environment from Docker Compose to Kubernetes.

Kubernetes has a command line interface for managing its clusters called kubectl.

As you probably guessed from the title, today I learned how to configure autocompletion for kubectl.

Kubernetes Logo

Setup

Depends on the shell you’re using.

zsh

Add the following to your .zshrc file:

source <(kubectl completion zsh)  # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell

If you’re using oh-my-zsh you can use this plugin by adding kubectl to the plugins array in your .zshrc:

plugins=(... kubectl)

bash

Add the following to your .bashrc or bash_profile:

source <(kubectl completion bash) # setup 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.

fish

Shahar Kedar pointed me to this repo.

Pro Tip

If you haven’t already, define this time-saving alias:

alias k="kubectl"
comments powered by Disqus