kubectl autocomplete with ZSH and aliases
19 Aug 2023 in TIL
This took me way too long to figure out.
How to make the k alias for kubectl autocomplete in zsh:
- Ensure that
setopt COMPLETE_ALIASESis set (this makes zsh expand the aliasktokubectlbefore completing, so it will use kubectl's completer automatically.) - Define your alias
alias k=kubectl - Load the
kubectlcompletions withsource <(kubectl completion zsh)
Here’s a complete .zshrc that works for me:
bashautoload -Uz compinitcompinitsetopt COMPLETE_ALIASESalias k=kubectlsource <(kubectl completion zsh)