LinuxCommandLibrary

kubectl-completion

Enable shell completion for kubectl commands

TLDR

Print the completion script for Bash, Zsh, fish, or PowerShell

$ kubectl completion [bash|zsh|fish|powershell]
copy

Load Bash or Zsh completions into the current shell session
$ source <(kubectl completion [bash|zsh])
copy

Append Bash completion script to ~/.bashrc
$ kubectl completion bash >> ~/.bashrc
copy

Write Zsh completion script to a file in the fpath
$ kubectl completion zsh > "${fpath[1]}/_kubectl"
copy

Load fish completions into the current shell session
$ kubectl completion fish | source
copy

Persist fish completions
$ kubectl completion fish > ~/.config/fish/completions/kubectl.fish
copy

Load PowerShell completions into the current shell session
$ kubectl completion powershell | Out-String | Invoke-Expression
copy

Persist PowerShell completions
$ kubectl completion powershell >> $PROFILE
copy

SYNOPSIS

kubectl completion bash|zsh|fish|powershell|cmd [--no-descriptions] [-h, --help]

PARAMETERS

-h, --help
    Display help for completion

--no-descriptions
    Omit completion descriptions for faster scripts

DESCRIPTION

kubectl completion generates shell-specific autocompletion scripts for the kubectl Kubernetes CLI, supporting Bash, Zsh, Fish, PowerShell, and CMD. It outputs code to stdout that enables intelligent tab-completion for subcommands, resources, namespaces, labels, and cluster-state data via API queries.

This dynamic completion reduces errors and speeds up workflows. Install by piping output to the shell's completion directory:

  • Bash: kubectl completion bash > /etc/bash_completion.d/kubectl
  • Zsh: kubectl completion zsh > ${fpath[1]}/_kubectl

Source the file or restart the shell. The --no-descriptions flag skips verbose hints for faster loading. Ideal for Linux environments, it integrates with package managers like bash-completion on Ubuntu/Fedora.

CAVEATS

Requires kubectl in PATH for dynamic sourcing. Generated files are static; re-run after kubectl upgrades. Zsh/Fish may need compinit reload.

BASH DYNAMIC SOURCING

Add to ~/.bashrc:
source <(kubectl completion bash)

ZSH PERMANENT INSTALL

kubectl completion zsh > "${fpath[1]}/_kubectl"
Then compinit

HISTORY

Introduced in Kubernetes v1.3.0 (March 2016) to boost CLI productivity; expanded shell support in later releases like v1.23+ for Fish/PowerShell.

SEE ALSO

kubectl(1)

Copied to clipboard