| name | kubectx |
| description | kubectx and kubens for Kubernetes context/namespace switching. Use for switching contexts/namespaces, fzf selection, shell completions, kube-ps1 prompts, KUBECONFIG management, or troubleshooting. |
| last_updated | "2026-03-18T00:00:00.000Z" |
kubectx & kubens
CLI tools by ahmetb for fast Kubernetes context and namespace switching. Written in Go, single binaries. Both support fzf for interactive fuzzy selection.
Installation
brew install kubectx
pacman -S kubectx
curl -Lo kubectx https://github.com/ahmetb/kubectx/releases/latest/download/kubectx
curl -Lo kubens https://github.com/ahmetb/kubectx/releases/latest/download/kubens
chmod +x kubectx kubens && mv kubectx kubens /usr/local/bin/
brew install fzf
kubectx - Context Switching
kubectx
kubectx <name>
kubectx -
kubectx -c
kubectx <new>=<old>
kubectx -d <name>
kubectx -d <name1> <name2>
kubectx -u
With fzf installed, bare kubectx opens interactive fuzzy finder instead of listing.
kubens - Namespace Switching
kubens
kubens <name>
kubens -
kubens -c
With fzf installed, bare kubens opens interactive fuzzy finder instead of listing.
How It Works
- kubectx modifies
~/.kube/config (or file(s) in $KUBECONFIG)
- Equivalent to
kubectl config use-context <name>
- kubens sets namespace in current context's config
- Equivalent to
kubectl config set-context --current --namespace=<name>
- The
- toggle stores previous selection in ~/.kube/kubectx and ~/.kube/kubens
KUBECONFIG with Multiple Files
export KUBECONFIG=~/.kube/config:~/.kube/cluster-a:~/.kube/cluster-b
kubectx
When KUBECONFIG contains multiple files, kubectx operates across all of them. Changes are written to the first file in the list that contains the modified context.
Common Patterns
Quick Context+Namespace Switch
kubectx production && kubens monitoring
Scripting (Non-Interactive)
KUBECTX_IGNORE_FZF=1 kubectx my-cluster
KUBECTX_IGNORE_FZF=1 kubens kube-system
current=$(kubectx -c)
kubectx staging
kubectx "$current"
Context Naming Convention
kubectx prod=arn:aws:eks:us-east-1:123456:cluster/production
kubectx staging=gke_myproject_us-central1_staging
kubectx homelab=kubernetes-admin@homelab
Reference Files
| File | Content |
|---|
references/shell-integration.md | zsh/bash completions, kube-ps1 prompt, fzf config, zinit setup |
Cross-References
- K3s clusters: Use
k3s skill for cluster setup, then kubectx to manage contexts
- Shell config: Use
zinit-zsh skill for plugin installation in this dotfiles repo