Shell completion makes kubectx and kubens even faster by providing tab-completion for context and namespace names. This guide covers setup for Bash, Zsh, and Fish shells.
Bash Completion
Installation
The completion scripts are located in the completion/ directory of the kubectx repository.
Locate completion scripts
If installed via package manager, completion scripts should be automatically installed. Otherwise: # Clone the repository
git clone https://github.com/ahmetb/kubectx.git
cd kubectx/completion
Source the completion scripts
Add to your ~/.bashrc or ~/.bash_profile: # kubectx completion
source /path/to/kubectx/completion/kubectx.bash
# kubens completion
source /path/to/kubectx/completion/kubens.bash
Package Manager Installation
Homebrew (macOS/Linux)
APT (Ubuntu/Debian)
# Completion is automatically set up
brew install kubectx
# Ensure bash-completion is installed
brew install bash-completion@2
# Add to ~/.bash_profile or ~/.bashrc:
export BASH_COMPLETION_COMPAT_DIR = "/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
Usage
Once configured, use Tab to complete context names:
kubectx mi < TA B >
# Expands to:
kubectx minikube
kubectx gke_ < TA B>< TA B >
# Shows all contexts starting with gke_:
gke_project_us-central1_dev
gke_project_us-central1_staging
gke_project_us-central1_prod
The - shorthand is also completed:
kubectx - < TA B >
# Completes to:
kubectx -
Zsh Completion
Installation
Locate completion scripts
Zsh completion scripts use the _ prefix: ls -l completion/_kubectx.zsh
ls -l completion/_kubens.zsh
Copy to fpath location
Copy completion scripts to a directory in your $fpath: # Create completion directory if it doesn't exist
mkdir -p ~/.zsh/completion
# Copy completion files
cp completion/_kubectx.zsh ~/.zsh/completion/_kubectx
cp completion/_kubens.zsh ~/.zsh/completion/_kubens
Update ~/.zshrc
Add the completion directory to your fpath before loading compinit: # Add to ~/.zshrc
fpath = ( ~/.zsh/completion $fpath )
autoload -U compinit
compinit
Oh My Zsh Installation
If you use Oh My Zsh:
# Copy to custom plugins directory
mkdir -p ~/.oh-my-zsh/custom/completions
cp completion/_kubectx.zsh ~/.oh-my-zsh/custom/completions/_kubectx
cp completion/_kubens.zsh ~/.oh-my-zsh/custom/completions/_kubens
# Reload
source ~/.zshrc
Package Manager Installation
Homebrew (macOS/Linux)
APT (Ubuntu/Debian)
# Install kubectx
brew install kubectx
# Completion is automatically set up via brew's fpath integration
# Ensure this is in your ~/.zshrc:
if type brew & > /dev/null; then
FPATH = $( brew --prefix ) /share/zsh/site-functions: $FPATH
autoload -U compinit
compinit
fi
Features
Zsh completion includes:
Context name completion
Support for - (switch to previous)
Support for -d flag with context names
Dynamic context list from kubectl config get-contexts
kubectx < TA B >
# Shows all available contexts
kubectx -d < TA B >
# Shows all contexts that can be deleted
kubens < TA B >
# Shows all namespaces in current context
Fish Completion
Installation
Locate completion scripts
Fish completion scripts: ls -l completion/kubectx.fish
ls -l completion/kubens.fish
Copy to Fish completions directory
# Create completions directory if it doesn't exist
mkdir -p ~/.config/fish/completions
# Copy completion files
cp completion/kubectx.fish ~/.config/fish/completions/
cp completion/kubens.fish ~/.config/fish/completions/
Reload completions
# Completions are loaded automatically
# Or manually reload:
fish_update_completions
Package Manager Installation
Homebrew (macOS/Linux)
Fisher (Fish plugin manager)
# Install kubectx
brew install kubectx
# Completions are automatically installed to:
# $(brew --prefix)/share/fish/vendor_completions.d/
Usage
Fish provides rich completion descriptions:
kubectx < TA B >
# Shows contexts with descriptions if available
kubectx - < TA B >
# Shows:
- (switch to the previous namespace in this context )
Alias Completion
If you use aliases for kubectx and kubens, you need to configure completion for the aliases as well.
Bash
# Add to ~/.bashrc
alias kctx = 'kubectx'
alias kns = 'kubens'
# Enable completion for aliases
complete -F _kube_contexts kctx
complete -F _kube_namespaces kns
Zsh
# Add to ~/.zshrc
alias kctx = 'kubectx'
alias kns = 'kubens'
# Enable completion for aliases
compdef kctx=kubectx
compdef kns=kubens
Fish
# Add to ~/.config/fish/config.fish
alias kctx= 'kubectx'
alias kns= 'kubens'
# Fish automatically handles completion for aliases
kubectl Plugin Completion
If you’re using kubectx/kubens as kubectl plugins:
# Completion works automatically through kubectl
kubectl ctx < TA B >
kubectl ns < TA B >
kubectl’s built-in completion handles plugin completion.
Verifying Completion
Test that completion is working:
Test kubectx completion
kubectx < TA B >
# Should show list of contexts
Test kubens completion
kubens < TA B >
# Should show list of namespaces
Test flag completion
kubectx - < TA B >
# Should show: - (switch to previous context)
Troubleshooting
Completion Not Working
If tab completion isn’t working:
Check if completion is loaded
# Check if completion function exists
type _kube_contexts
# Should show: _kube_contexts is a function
Verify shell integration
Make sure bash-completion or zsh completion system is enabled: # Bash: check if bash-completion is loaded
type _init_completion
# Zsh: check if compinit is called
whence compinit
Reload shell configuration
Completions Out of Date
If new contexts/namespaces don’t appear in completion:
# Completions are generated dynamically
# They call: kubectl config get-contexts
# Verify kubectl can list contexts:
kubectl config get-contexts --output=name
# If this works, completion should work too
Permission Errors
If you see permission errors:
# Make sure completion files are readable
chmod +r ~/.zsh/completion/_kubectx
chmod +r ~/.config/fish/completions/kubectx.fish
Advanced Configuration
Custom Completion Behavior
You can modify the completion scripts to customize behavior. For example, in Bash:
# Custom completion that excludes certain contexts
_kube_contexts_filtered ()
{
local curr_arg ;
curr_arg = ${ COMP_WORDS [COMP_CWORD]}
local contexts = $( kubectl config get-contexts --output= 'name' | grep -v 'test' )
COMPREPLY = ( $( compgen -W "- $contexts " -- $curr_arg ) );
}
complete -F _kube_contexts_filtered kubectx
For large kubeconfig files, completion might be slow. Consider:
Caching contexts (not implemented by default)
Using fzf interactive mode instead of tab completion
Splitting kubeconfig into smaller files
Interactive mode with fzf is often faster than tab completion for large context lists.
Next Steps
Interactive Mode Learn about fzf integration for visual selection
kubectx Commands Master all kubectx commands and features