- 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/kubectl
Creates a symlink from
minikube
tokubectl
, so when you typekubectl
it will run minikube instead. I assumeminikube
checks if the command ran iskubectl
and interpret it asminikube kubectl --
instead the originalminikube
command. Using thealias
approach (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
minikube
andkubectl
respectively.alias k="kubectl"
Creates an alias for
kubectl
tok
.compdef _kubectl k
Tells zsh to use the completion script for
kubectl
for thek
alias.