WSLのターミナルの表示を良い感じにする


概要

最近PCが壊れてリカバリしたばっかりなので環境構築ネタの中から1つ。
WSL(Ubuntu)のpromptの標準表示がイケてないので改善していきます。

Before


※白塗りはマシン名

Beforeの課題点
- Windowsのフォルダ表示が「背景緑」「文字青」でとても見づらい
- WSLでユーザー名やマシン名が見えても特にうれしくない
- フルパス表示は長すぎる
- gitの管理下であればカレントのbranch名も出してほしい

After

Afterの改善点
- Windowsのフォルダ表示が「背景水色」「文字黒」に変更
- ユーザー名やマシン名を削る
- フルパスからフォルダ名表示のみに変更
- カレントのbranch名を出力
- ついでにホワイトエンジニアたるもの時間を常に意識する気持ちは大事なので時刻表示を追加

やりかた

  • git-promptをダウンロード

git branch名を取得するためにgit公式が提供しているgit-promptをダウンロードしてきます。

cd ~ && curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -o ".git-prompt.sh" && chmod +x .git-prompt.sh
  • dirclorsをファイルに出力
dircolors -p > ~/.dircolors
  • dircolorsを編集
~/.dircolors
- OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
+ #OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
+ OTHER_WRITABLE 30;46
- STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
+ #STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
+ STICKY 30;44
  • bashrcを書き換え
~/.bashrc
+source ~/.git-prompt.sh
 if [ "$color_prompt" = yes ]; then
-    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
+#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
+    PS1='\[\e[30;47m\] \t \[\e[30;46m\] \W $(__git_ps1 "\[\e[30;42m\](%s)")\[\e[0m\]\$ '
 else
-    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
+#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
+    PS1='\t \W $(__git_ps1 "(%s)") \$'
 fi
+
+# dircolors
+eval $(dircolors -b ~/.dircolors)
  • 反映
source ~/.bashrc
  • WSLでの表示

  • Windows Terminalでの表示

ちなみにgit管理ディレクトリではない場合にはgit branchは出力しないようにしています。
ターミナルによって若干色は違いますが見やすくはなっていると思います。

みなさんもpromptをカスタマイズして良いハックライフを!

参考記事

ターミナルプロンプトの表示・色の変更 - Qiita
WSLのWindowsのフォルダの色が見づらいのを直す