Skip to content

Instantly share code, notes, and snippets.

@rrotter
Created May 24, 2024 21:39
Show Gist options
  • Save rrotter/a45f5fea7a21b6ab24f51be67bf036f5 to your computer and use it in GitHub Desktop.
Save rrotter/a45f5fea7a21b6ab24f51be67bf036f5 to your computer and use it in GitHub Desktop.
zsh completion for kubectl and anything else
# Adding the following to the _end_ of your zshrc should
# (1) enable completion for most commands
# (2) work on any os zsh and kubectl can be installed on
# (3) enable completion for kubectl even if some common configuration issues are present
# (4) enable zsh completion for aws and tk
# (5) be safe to append to .zshrc without a further thought (please don't)
#
# That being said, you almost certainly don't need every line in this file.
# Reading through this should help you understand zsh completion setup enough
# to choose what to copy for your own configuration.
if type brew &>/dev/null; then
# HOMEBREW_PREFIX should already be exported, export if it's missing
[[ -z $HOMEBREW_PREFIX ]] && export HOMEBREW_PREFIX=$(brew --prefix)
# tell zsh where homebrew's zsh completers live
fpath+=${HOMEBREW_PREFIX}/share/zsh/site-functions
fi
# Set up zsh completion. If your your os, zsh, package manager, and all
# commands are well configured, this ONE line should be all you need.
# This should only run once, and must run AFTER $fpath is fully configured,
# so make sure this isn't already run somewhere else in your .zshrc, and
# consider keeping it near the end of .zshrc.
autoload -Uz compinit && compinit
# Source the kubectl completer if it's missing (i.e. if _kubectl isn't in any
# path in $fpath). Why would this happen?
# - certain packaged versions (k8s apt repo *ahem*) don't include completion
# - kubectl manually installed
# - completer is installed, but $fpath not set correctly
#
# In any case, there is nothing wrong with this method save for adding a few
# milliseconds to shell init.
type kubectl &>/dev/null && ! type _kubectl &>/dev/null && source <(kubectl completion zsh)
# Very rarely a command only has bash completion support. Check with the
# vendor website, stackoverflow, reddit, etc before assuming this is the case
# as it's really rare that a command doesn't have native zsh completion support
# even from a 3rd party source. But when this is the case, like w/ awscli...
# bashcompinit: This must be run exactly once, and should run after compinit,
# but only if you are installing bash completers.
autoload -Uz bashcompinit && bashcompinit
# confirm that bash completer is in $PATH, then set it up
# note: homebrew ships a _aws zsh completer, but
# it's just a broken wrapper for `aws_completer`
type aws_completer &>/dev/null && complete -C aws_completer aws
# If `aw<tab>` uselessly gives you the aws_completer as an option in addition to
# aws, this will fix it.
zstyle ':completion::complete:-command-:*:*' ignored-patterns 'aws_completer'
# bonus: install completion for tanka
type tk &>/dev/null && complete -C tk tk
# troubleshooting and notes:
# - Try `print -l $fpath` to inspect $fpath. Also see: `print -l $path`.
# - note $FPATH and $fpath are different views into the same variable
# - same w/ $PATH and $path
# - `print` is `echo` with different options
# - `type` is `which` with different options and output formatting
# - `type foobar &>/dev/null` checks if foobar is in $PATH,
# and redirects BOTH stdout and stderr to /dev/null
# - I'm using vanilla zsh. If you are using a oh-my-zsh you are probably better
# off looking at their support forums.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment