master cc8e2d15b56c cached
4 files
7.5 KB
2.3k tokens
1 requests
Download .txt
Repository: superbrothers/zsh-kubectl-prompt
Branch: master
Commit: cc8e2d15b56c
Files: 4
Total size: 7.5 KB

Directory structure:
gitextract__r_x8kl6/

├── LICENSE.txt
├── README.md
├── kubectl.zsh
└── zsh-kubectl-prompt.plugin.zsh

================================================
FILE CONTENTS
================================================

================================================
FILE: LICENSE.txt
================================================
The MIT License (MIT)

Copyright (c) 2017  Kazuki Suda <kazuki.suda@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


================================================
FILE: README.md
================================================
# zsh-kubectl-prompt

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

![Screenshot](./images/screenshot001.png)

## 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_USER

For 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
  fi

  echo "%{$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
fi

autoload -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 apply

autoload -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.


================================================
FILE: kubectl.zsh
================================================
setopt prompt_subst
autoload -U add-zsh-hook

function() {
    local namespace separator binary

    # Specify the separator between context and namespace
    zstyle -s ':zsh-kubectl-prompt:' separator separator
    if [[ -z "$separator" ]]; then
        zstyle ':zsh-kubectl-prompt:' separator '/'
    fi

    # Display the current namespace if `namespace` is true
    zstyle -s ':zsh-kubectl-prompt:' namespace namespace
    if [[ -z "$namespace" ]]; then
        zstyle ':zsh-kubectl-prompt:' namespace true
    fi

    # Specify the binary to get the information from kubeconfig (e.g. `oc`)
    zstyle -s ':zsh-kubectl-binary:' binary binary
    if [[ -z "$binary" ]]; then
        zstyle ':zsh-kubectl-prompt:' binary "kubectl"
    fi
}

add-zsh-hook precmd _zsh_kubectl_prompt_precmd
function _zsh_kubectl_prompt_precmd() {
    local kubeconfig config updated_at now context namespace ns separator modified_time_fmt binary

    zstyle -s ':zsh-kubectl-prompt:' binary binary
    if ! command -v "$binary" >/dev/null; then
      ZSH_KUBECTL_PROMPT="${binary} command not found"
      return 1
    fi

    kubeconfig="$HOME/.kube/config"
    if [[ -n "$KUBECONFIG" ]]; then
        kubeconfig="$KUBECONFIG"
    fi

    zstyle -s ':zsh-kubectl-prompt:' modified_time_fmt modified_time_fmt
    if [[ -z "$modified_time_fmt" ]]; then
      # Check the stat command because it has a different syntax between GNU coreutils and FreeBSD.
      if stat --help >/dev/null 2>&1; then
          modified_time_fmt='-c%y' # GNU coreutils
      else
          modified_time_fmt='-f%m' # FreeBSD
      fi
      zstyle ':zsh-kubectl-prompt:' modified_time_fmt $modified_time_fmt
    fi

    # KUBECONFIG environment variable can hold a list of kubeconfig files that is colon-delimited.
    # Therefore, if KUBECONFIG has been held multiple files, each files need to be checked.
    while read -d ":" config; do
        if ! now="${now}$(stat -L $modified_time_fmt "$config" 2>/dev/null)"; then
            ZSH_KUBECTL_PROMPT="$config doesn't exist"
            return 1
        fi
    done <<< "${kubeconfig}:"

    zstyle -s ':zsh-kubectl-prompt:' updated_at updated_at
    if [[ "$updated_at" == "$now" ]]; then
        return 0
    fi
    zstyle ':zsh-kubectl-prompt:' updated_at "$now"

    # Set environment variable if context is not set
    if ! context="$("$binary" config current-context 2>/dev/null)"; then
        ZSH_KUBECTL_PROMPT="current-context is not set"
        return 1
    fi

    ZSH_KUBECTL_USER="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.user}")"
    ZSH_KUBECTL_CONTEXT="${context}"
    ns="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
    [[ -z "$ns" ]] && ns="default"
    ZSH_KUBECTL_NAMESPACE="${ns}"

    # Specify the entry before prompt (default empty)
    zstyle -s ':zsh-kubectl-prompt:' preprompt preprompt
    # Specify the entry after prompt (default empty)
    zstyle -s ':zsh-kubectl-prompt:' postprompt postprompt

    # Set environment variable without namespace
    zstyle -s ':zsh-kubectl-prompt:' namespace namespace
    if [[ "$namespace" != true ]]; then
        ZSH_KUBECTL_PROMPT="${preprompt}${context}${postprompt}"
        return 0
    fi

    zstyle -s ':zsh-kubectl-prompt:' separator separator
    ZSH_KUBECTL_PROMPT="${preprompt}${context}${separator}${ns}${postprompt}"

    return 0
}


================================================
FILE: zsh-kubectl-prompt.plugin.zsh
================================================
source ${0:A:h}/kubectl.zsh
Download .txt
gitextract__r_x8kl6/

├── LICENSE.txt
├── README.md
├── kubectl.zsh
└── zsh-kubectl-prompt.plugin.zsh
Condensed preview — 4 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (8K chars).
[
  {
    "path": "LICENSE.txt",
    "chars": 1103,
    "preview": "The MIT License (MIT)\n\nCopyright (c) 2017  Kazuki Suda <kazuki.suda@gmail.com>\n\nPermission is hereby granted, free of ch"
  },
  {
    "path": "README.md",
    "chars": 3130,
    "preview": "# zsh-kubectl-prompt\n\nThis script displays information about the kubectl current context and namespace in zsh prompt.\n\n!"
  },
  {
    "path": "kubectl.zsh",
    "chars": 3419,
    "preview": "setopt prompt_subst\nautoload -U add-zsh-hook\n\nfunction() {\n    local namespace separator binary\n\n    # Specify the separ"
  },
  {
    "path": "zsh-kubectl-prompt.plugin.zsh",
    "chars": 28,
    "preview": "source ${0:A:h}/kubectl.zsh\n"
  }
]

About this extraction

This page contains the full source code of the superbrothers/zsh-kubectl-prompt GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 4 files (7.5 KB), approximately 2.3k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!