kubectx and kubens support interactive mode through fzf , a command-line fuzzy finder. This provides a visual, searchable interface for selecting contexts and namespaces.
Prerequisites
Interactive mode requires fzf to be installed on your system.
Install fzf
macOS (Homebrew)
Ubuntu/Debian
Fedora
From source
Once fzf is installed, kubectx and kubens will automatically detect it and enable interactive mode when invoked without arguments in a TTY.
Interactive Context Selection
Basic Usage
When you run kubectx without arguments in an interactive terminal with fzf installed, it enters interactive mode:
This opens an interactive fuzzy finder with all your contexts:
>
10/10
> minikube
gke_project_us-central1_dev
gke_project_us-central1_staging
gke_project_us-central1_prod
arn:aws:eks:us-west-2:123456789012:cluster/dev
arn:aws:eks:us-west-2:123456789012:cluster/staging
arn:aws:eks:us-west-2:123456789012:cluster/prod
docker-desktop
kind-local
k3d-test
The current context is highlighted with a different color.
Navigation
Type to search Start typing to filter contexts by name > prod
2/10
> gke_project_us-central1_prod
arn:aws:eks:us-west-2:123456789012:cluster/prod
Arrow keys Use ↑/↓ to navigate through results
Enter to select Press Enter to switch to the highlighted context
Esc to cancel Press Esc or Ctrl+C to exit without changes
Example Session
Search for context
Type prod to filter: > prod
2/10
> gke_project_us-central1_prod
arn:aws:eks:us-west-2:123456789012:cluster/prod
Select context
Press Enter on gke_project_us-central1_prod Output: Switched to context "gke_project_us-central1_prod".
Interactive Namespace Selection
Basic Usage
Similarly, run kubens without arguments to enter interactive mode:
This opens an interactive fuzzy finder with all namespaces in the current context:
>
15/15
> default
kube-system
kube-public
kube-node-lease
production
staging
development
monitoring
logging
ingress-nginx
cert-manager
feature-auth
feature-payments
feature-notifications
qa-testing
The current active namespace is highlighted.
Example Session
Search for namespace
Type feature to filter: > feature
3/15
> feature-auth
feature-payments
feature-notifications
Select namespace
Press Enter on feature-auth Output: Active namespace is "feature-auth".
Interactive Delete
kubectx also supports interactive deletion of contexts:
This opens the same fzf interface, but selecting a context will delete it:
Use interactive delete mode with caution. Once you select a context, it will be immediately deleted from your kubeconfig.
Launch interactive delete
Find context to delete
Search and navigate to the context you want to remove: > old
2/10
> old-dev-cluster
old-test-cluster
Confirm deletion
Press Enter to delete the selected context Output: Deleted context old-dev-cluster.
If you delete the currently active context:
You deleted the current context. Use "kubectx" to select a new context.
Deleted context production.
Customizing fzf Behavior
Environment Variables
You can customize fzf behavior using environment variables:
# Set default fzf options
export FZF_DEFAULT_OPTS = '--height 40% --reverse --border'
# Now kubectx/kubens will use these options
kubectx
Custom fzf Options
kubectx and kubens use specific fzf flags:
--ansi: Enable ANSI color codes (for highlighting current context/namespace)
--no-preview: Disable preview window
These are hardcoded to ensure consistent behavior.
Color Output
Interactive mode automatically enables color output to highlight:
The current context (in green)
The current namespace (in green)
Search matches (fzf default highlighting)
Colors are applied through the KUBECTL_CONTEXT_FORCE_COLOR environment variable, which is automatically set when fzf is invoked.
Non-Interactive Fallback
If fzf is not installed or not in PATH, kubectx and kubens fall back to non-interactive mode:
# Without fzf installed
kubectx
# Lists contexts (non-interactive)
minikube
gke_project_us-central1_dev
gke_project_us-central1_staging
Interactive mode requires both fzf and a TTY (terminal). When piping output or running in scripts, kubectx/kubens automatically use non-interactive mode.
Tips and Tricks
Fuzzy Matching
fzf uses fuzzy matching, so you don’t need exact strings:
# Searching for "gke_project_us-central1_prod"
# You can type:
type : gkeprod
type : gprod
type : gc1p
# All will match!
Quick Selection
For frequently used contexts/namespaces, type just enough characters to make them unique:
kubectx
# Type "mi" - if "minikube" is the only match, press Enter
Multi-Select (Not Supported)
kubectx and kubens do not currently support multi-select in fzf. You can only select one context or namespace at a time.
Troubleshooting
fzf Not Found
If you see:
kubectx
# Lists contexts without interactive mode
Check if fzf is in your PATH:
which fzf
# /usr/local/bin/fzf (if installed)
# (no output if not found)
Install fzf following the Prerequisites section.
No Colors in Interactive Mode
If the current context isn’t highlighted:
# Check if color output is working
kubectx
# Current context should be in color
# Force color output
export KUBECTL_CONTEXT_FORCE_COLOR = 1
kubectx
Interactive Mode in Scripts
Interactive mode is automatically disabled in non-TTY environments:
#!/bin/bash
# This will use non-interactive mode
CONTEXT = $( kubectx | grep prod )
To force non-interactive mode in a TTY:
# Pipe to cat to disable TTY detection
kubectx | cat
Next Steps
kubectx Commands Learn all kubectx commands and options
kubens Commands Learn all kubens commands and options