Skip to content
Display information about the kubectl current context and namespace in zsh prompt.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
images First commit Apr 2, 2017
LICENSE.txt First commit Apr 2, 2017
README.md Add optional vars before and after the prompt (#16) Jun 3, 2019
kubectl.zsh Add optional vars before and after the prompt (#16) Jun 3, 2019
zsh-kubectl-prompt.plugin.zsh Enable the use of oh-my-zsh (#14) Apr 21, 2019

README.md

zsh-kubectl-prompt

This script displays information about the kubectl current context and namespace in zsh prompt.

Screenshot

Usage

Clone this repository and source the kubectl.zsh from your ~/.zshrc config file, and configure your prompt.

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_USER

For example, make the prompt red when the username matches admin.

autoload -U colors; colors
source /path/to/zsh-kubectl-prompt/kubectl.zsh
[[ "$ZSH_KUBECTL_USER" =~ "admin" ]] && color=red || color=blue
RPROMPT='%{$fg[$color]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'

Also you can install with homebrew.

$ brew tap superbrothers/zsh-kubectl-prompt
$ brew install zsh-kubectl-prompt

Customization

Change the separator between context and namespace:

zstyle ':zsh-kubectl-prompt:' separator '|'

Add custom character before the prompt:

zstyle ':zsh-kubectl-prompt:' preprompt '<'

Add custom character after the prompt:

zstyle ':zsh-kubectl-prompt:' postprompt '>'

Does not display the current namespace:

zstyle ':zsh-kubectl-prompt:' namespace false

With a plugin manager

If you use zgen, load this repository as follows:

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
fi

autoload -U colors; colors
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'

If you use antigen, load this repository as follows:

source /path-to-antigen/antigen.zsh

# load this plugin
antigen bundle superbrothers/zsh-kubectl-prompt

# tell antigen that you're done.
antigen apply

autoload -U colors; colors
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'

If you use oh-my-zsh, load this repository as follows:

  1. Clone the repo into oh-my-zsh custom plugins folder
git clone git@github.com:superbrothers/zsh-kubectl-prompt.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-kubectl-prompt
  1. Activate the plugin your .zshrc by appending it to the plugin section
plugins=( [plugins...] zsh-kubectl-prompt)

Note: Remember to source the .zshrc or restart your shell after step 2

License

This script is released under the MIT License.

You can’t perform that action at this time.