2024/03/25

カレントディレクトリパスの中のgitレポジトリに色を付けると便利

[bash] [ps1] [git]

PS1 があるじゃろ?

PS1="\u \w \$(__git_ps1 \": %s\")\n$ "

bayashi ユーザのホームディレクトリならこんな表示じゃ

bayashi ~
$ 

なんの変哲もないプロンプトじゃな

PS1 の中身を書き下すとこうじゃ

  • \u ログインユーザ
  • \w カレントディレクトリ
  • $(__git_ps1 ": %s") gitディレクトリの場合、ブランチ名
  • \n$ 改行してプロンプトを示す $ を表示

たとえば、~/gitrepo/bayashi/Foo という gitリポジトリに移動するとこうなる

normal prompt

おもむろに tool をインストールするのじゃ

go install github.com/bayashi/highlightrepo@latest

そして PS1 の \w$(pwd | highlightrepo -y) に置き換えるのじゃ

PS1="\u \$(pwd | highlightrepo -y) \$(__git_ps1 \": %s\")\n$ "

gitリポジトリが自動的にハイライトされるじゃろ

highlighted git repository

筋斗雲じゃ これをおまえにやろう!

https://github.com/bayashi/highlightrepo

サイト内検索