3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kubectl の current-context/namespace を zsh のプロンプトに出力する

Posted at

zsh に git のブランチ名を出力しているのですが、kubectl の current-context や namespace も出力できると良いなと思ってやって見ました。

初めは kubectl コマンドを使ってやっていたのですが、やや遅くてコマンドを実行するたびにストレスを感じるようになったので、Go で ~/.kube/config を読み込んで出力するだけのコマンド kube_prompt_info を作ってやって見ました。

ちなみにこの名前は oh-my-zsh 付属の git_prompt_info が元になっており、kube-prompt とは関係ありません。

最終的にこんな感じになります。
image.png

kube_prompt_info のインストール

Go をインストールした状態で以下を実行します。

$ go get github.com/yuya-takeyama/kube_prompt_info

kube_prompt_info のセットアップ

zsh には $PROMPT という変数を書き換えることでプロンプトを書き換えられます。

~/.zshrc に以下のような行を書き加えます。

PROMPT='%m:%c $(kube_prompt_info) %(!.#.$) '

export KUBE_PROMPT_INFO_PREFIX="%{$fg[yellow]%}("
export KUBE_PROMPT_INFO_SUFFIX=")%{$reset_color%}"

この状態で source ~/.zshrc するなどすれば完了。

私は実際には oh-my-zsh 付属の daveverwer という theme をコピーして中身をいじって、以下のような感じのプロンプトになっています。
image.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?