Fast Kubernetes context & namespace switcher — a delightful TUI plus scriptable subcommands, in a single Go binary.
ktx makes hopping between clusters and namespaces instant. Launch it with no
arguments for a fuzzy-filterable picker, or use the subcommands in scripts and
prompts. It only edits your kubeconfig file — it never dials a cluster, so it
is fast and safe to run anywhere.
Running ktx opens the interactive picker. Type to fuzzy-filter, use the arrow
keys to move, and press Enter to switch. Your current context is
marked with a green dot.
ktx · switch kubecontext
▸ dev ●
cluster: dev-cluster · ns: default · user: dev-user
prod
cluster: prod-cluster · ns: payments · user: prod-user
staging
cluster: dev-cluster · ns: web · user: dev-user
enter: switch · /: filter · q: quit
Prefer one-liners? Everything the TUI does is available non-interactively:
$ ktx list
CURRENT NAME CLUSTER NAMESPACE USER
* dev dev-cluster default dev-user
prod prod-cluster payments prod-user
staging dev-cluster web dev-user
$ ktx use prod
Switched to context "prod"
$ ktx ns billing
Set namespace "billing" on context "prod"
$ ktx current
prod (namespace: billing)brew install HariBoddapati01/tap/ktxgo install github.com/HariBoddapati01/ktx@latestGrab an archive for your OS/arch from the
releases page, extract it, and
put ktx on your PATH.
git clone https://github.com/HariBoddapati01/ktx.git
cd ktx
make installktx Launch the interactive context picker
ktx list [--quiet] List all contexts (--quiet prints names only)
ktx use <context> Switch the current context
ktx ns <namespace> Set the namespace on the current context
ktx ns <ns> -c <ctx> ...or on a specific context
ktx current [--namespace] Show the current context (and namespace)
ktx --version Print the version
ktx follows the standard client-go rules:
- the
$KUBECONFIGenvironment variable (a:-separated list is honoured), then ~/.kube/config.
Writes go back to the file that owns the setting via
clientcmd.ModifyConfig, so merged multi-file kubeconfigs are respected and
untouched files are left alone.
alias kx='ktx use'
alias kn='ktx ns'
# Show the active context/namespace in your prompt:
kctx() { ktx current 2>/dev/null; }- No cluster calls. Pure kubeconfig manipulation means it works offline and can't hang on an unreachable API server.
- Scriptable + interactive. The same logic backs both the TUI and the CLI.
- Small and dependency-light at runtime. A single static binary.
- Tested where it counts. The kubeconfig logic lives in
internal/kubewith pure functions and thorough unit tests against in-memory configs.
make # tidy, fmt, vet, lint, test, build
make test # go test ./... -race -coverprofile=cover.out
make lint # golangci-lint
make build # build ./bin/ktx with version stamped in
make snapshot # local goreleaser snapshot (no publish).
├── main.go # entrypoint; version injected via -ldflags
└── internal
├── cli # cobra command tree
├── kube # pure kubeconfig logic + loader (unit tested)
└── tui # Bubble Tea context picker
The interesting, well-tested code is in
internal/kube: ListContexts, SwitchContext, and
SetNamespace operate on an in-memory *clientcmdapi.Config, which keeps them
trivial to test without touching the filesystem or a cluster.
Apache-2.0 © Hari Boddapati