- Published on
 
How to use minikube kubectl completion on zsh
2 min read
- Authors
 - Name
 - Syakhisk Al Azmi
 - @syakhiskk
 
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/kubectlCreates a symlink from
minikubetokubectl, so when you typekubectlit will run minikube instead. I assumeminikubechecks if the command ran iskubectland interpret it asminikube kubectl --instead the originalminikubecommand. Using thealiasapproach (alias kubectl='minikube kubectl --') didn't generate the correct alias.source <(minikube completion zsh)andsource <(kubectl completion zsh)This will load the completion script for
minikubeandkubectlrespectively.alias k="kubectl"Creates an alias for
kubectltok.compdef _kubectl kTells zsh to use the completion script for
kubectlfor thekalias.