Published on

How to use minikube kubectl completion on zsh

2 min read

Authors

TLDR; Add this to your .zshrc:

[[ ! -f "/usr/local/bin/kubectl" ]] && sudo ln -s $(which minikube) /usr/local/bin/kubectl

source <(minikube completion zsh)
source <(kubectl completion zsh)

alias k="kubectl"
compdef _kubectl k

If you get an error like 2: command not found: compdef, then add the following before the snippet above:

autoload -Uz compinit
compinit

Background

I'm using minikube to run a local kubernetes cluster, and I'm using zsh as my shell. I want to use the kubectl completion feature, so I can type k get po and press tab to complete the command.

I've tried to follow the official documentation but it doesn't work. I've also tried to follow this issue but there seem no clear way how to do it.

After some trial and error, I finally found the solution.

Explanation

  • [[ ! -f "/usr/local/bin/kubectl" ]] && sudo ln -s $(which minikube) /usr/local/bin/kubectl

    Creates a symlink from minikube to kubectl, so when you type kubectl it will run minikube instead. I assume minikube checks if the command ran is kubectl and interpret it as minikube kubectl -- instead the original minikube command. Using the alias approach (alias kubectl='minikube kubectl --') didn't generate the correct alias.

  • source <(minikube completion zsh) and source <(kubectl completion zsh)

    This will load the completion script for minikube and kubectl respectively.

  • alias k="kubectl"

    Creates an alias for kubectl to k.

  • compdef _kubectl k

    Tells zsh to use the completion script for kubectl for the k alias.

Resources