Fixing kubectl autocompletion for an alias in Zsh

August 5, 2019 · less than 1 minute read

With the upcoming switch in MacOS to use Zsh rather than Bash, I decided to Zsh a shot as well for my setup. The setup (Zsh + oh-my-zsh) has been great so far. However, autocompletion for my alias of kubectl (k) using the common way to setup aliases with autocompletion caused a segfault in the shell.

alias k="kubectl"
alias compdef k="kubectl"

Since probably many people hit this issue at some point, I figured I’d share the workaround that I am using to have a kubectl alias with autocompletion.

So, to work around this issue, I sourced the autocompletion of kubectl manually, replacing the kubectl command with the k alias:

alias k="kubectl"
source <(kubectl completion zsh | sed 's/kubectl/k/g')