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 1 year has passed since last update.

terminalをいい感じに色分けする!!!!!

Posted at

この記事でできること

terminalを↓な感じで表示できる。ちょっとみやすい。
スクリーンショット 2024-06-08 21.44.30.png

やりかた

自由にカスタマイズできるよう、ステップに分けて説明していきます。

wgetを使用可能にする

まず、今回 wget を利用するので、必要な方はインストールします。

brew install wget

諸々のコマンドを実行する

いきなりですが、コピペで動くコマンド群を載せちゃいます。

mkdir -p ~/usr/local/git/contrib/completion
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/usr/local/git/contrib/completion/git-prompt.sh
echo source ~/usr/local/git/contrib/completion/git-prompt.sh >> ~/.bashrc
echo GIT_PS1_SHOWDIRTYSTATE=true >> ~/.bashrc
echo export PS1=\'\\[\\e[1\;32m\\]\\u:\\[\\e[1\;36m\\]\\W\\[\\e[1\;33m\\]\$\(__git_ps1\)\\[\\033[00m\\]\$ \' >> ~/.bashrc
source ~/.bashrc  

ユーザー名:緑
ディレクトリ名:青
ブランチ名:黄色

で示されるようになりました!

これだけだとまぁ寂しいので、それぞれのコマンドで何をやっているか、解説を挟んでいきますので、よければお読みください :bow:

解説

  • Gitのスクリプトを保存するためのディレクトリを作成します。
mkdir -p ~/usr/local/git/contrib/completion
  • Gitのプロンプト設定スクリプトをダウンロードします。
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/usr/local/git/contrib/completion/git-prompt.sh
  • Gitのプロンプト設定を読み込むための設定を.bashrcに追加します
echo source ~/usr/local/git/contrib/completion/git-prompt.sh >> ~/.bashrc
  • Gitプロンプトにリポジトリの状態(変更済みファイルの有無)を表示するように設定します
echo GIT_PS1_SHOWDIRTYSTATE=true >> ~/.bashrc
  • terminalの色の設定を行います
echo export PS1=\'\\[\\e[1\;32m\\]\\u:\\[\\e[1\;36m\\]\\W\\[\\e[1\;33m\\]\$\(__git_ps1\)\\[\\033[00m\\]\$ \' >> ~/.bashrc  

\\[\\e[1\;32m\\]\\u: ユーザー名を緑色で表示
\\[\\e[1\;36m\\]\\W: 現在のディレクトリをシアン色で表示
\\[\\e[1\;33m\\]\$\(__git_ps1\): Gitリポジトリの状態を黄色で表示
\\[\\033[00m\\]\$: それ以降をデフォルトの色に戻す


  • 最後にbashrcを再読み込みして、設定を反映する
source ~/.bashrc

おわりに

上記のように、terminalの表示色を任意に変更することができます!
この情報も入れたい、この色に変更したいなど、ChatGPTさんと聞けば答えてくれると思うので、自分にあった表示を探してみてください!

ありがとうございました。

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?