https://github.com/superbrothers/zsh-kubectl-prompt
Display information about the kubectl current context and namespace in zsh prompt.
https://github.com/superbrothers/zsh-kubectl-prompt
kubectl kubernetes prompt zsh
Last synced: 17 days ago
JSON representation
Display information about the kubectl current context and namespace in zsh prompt.
- Host: GitHub
- URL: https://github.com/superbrothers/zsh-kubectl-prompt
- Owner: superbrothers
- License: mit
- Created: 2017-03-28T00:37:03.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-05-15T02:43:09.000Z (almost 2 years ago)
- Last Synced: 2025-03-28T19:09:44.360Z (24 days ago)
- Topics: kubectl, kubernetes, prompt, zsh
- Language: Shell
- Homepage:
- Size: 36.1 KB
- Stars: 573
- Watchers: 6
- Forks: 40
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# zsh-kubectl-prompt
This script displays information about the kubectl current context and namespace in zsh prompt.

## Usage
Clone this repository and source the `kubectl.zsh` from your `~/.zshrc` config file, and configure your prompt.
```sh
autoload -U colors; colors
source /path/to/zsh-kubectl-prompt/kubectl.zsh
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'
```Or create different style depending on user, context, namespace.
The plugin creates 4 variables:
* ZSH_KUBECTL_CONTEXT
* ZSH_KUBECTL_NAMESPACE
* ZSH_KUBECTL_PROMPT
* ZSH_KUBECTL_USERFor example, make the prompt red when the username matches admin.
```sh
autoload -U colors; colors
source /path/to/zsh-kubectl-prompt/kubectl.zsh
function right_prompt() {
local color="blue"if [[ "$ZSH_KUBECTL_USER" =~ "admin" ]]; then
color=red
fiecho "%{$fg[$color]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}"
}
RPROMPT='$(right_prompt)'
```Also you can install with homebrew.
```sh
brew tap superbrothers/zsh-kubectl-prompt
brew install zsh-kubectl-prompt
```## Customization
Change the separator between context and namespace:
```sh
zstyle ':zsh-kubectl-prompt:' separator '|'
```Add custom character before the prompt:
```sh
zstyle ':zsh-kubectl-prompt:' preprompt '<'
```Add custom character after the prompt:
```sh
zstyle ':zsh-kubectl-prompt:' postprompt '>'
```Does not display the current namespace:
```sh
zstyle ':zsh-kubectl-prompt:' namespace false
```Use another binary instead of `kubectl` to get the information (e.g. `oc`):
```sh
zstyle ':zsh-kubectl-prompt:' binary 'oc'
```## With a plugin manager
If you use [zgen](https://github.com/tarjoilija/zgen), load this repository as follows:
```sh
source "${HOME}/.zgen/zgen.zsh"# if the init script doesn't exist
if ! zgen saved; then
# specify plugins here
zgen load superbrothers/zsh-kubectl-prompt# generate the init script from plugins above
zgen save
fiautoload -U colors; colors
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'
```If you use [antigen](https://github.com/zsh-users/antigen), load this repository as follows:
```sh
source /path-to-antigen/antigen.zsh# load this plugin
antigen bundle superbrothers/zsh-kubectl-prompt# tell antigen that you're done.
antigen applyautoload -U colors; colors
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'
```If you use [oh-my-zsh](https://ohmyz.sh/), load this repository as follows:
1. Clone the repo into oh-my-zsh custom plugins folder
```sh
git clone https://github.com/superbrothers/zsh-kubectl-prompt.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-kubectl-prompt
```2. Activate the plugin your `.zshrc` by appending it to the plugin section
```sh
plugins=( [plugins...] zsh-kubectl-prompt)
```3. Configure your prompt (or check how to customize the theme plugin you are using)
```sh
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'
```> **Note:** Remember to source the `.zshrc` or restart your shell after step 2
## License
This script is released under the MIT License.