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:

  1. Ensure that setopt COMPLETE_ALIASES is set (this makes zsh expand the alias k to kubectl before completing, so it will use kubectl's completer automatically.)
  2. Define your alias alias k=kubectl
  3. Load the kubectl completions with source <(kubectl completion zsh)

Here’s a complete .zshrc that works for me:

bash
autoload -Uz compinit
compinit
setopt COMPLETE_ALIASES
alias k=kubectl
source <(kubectl completion zsh)