LoginSignup
12
12

More than 5 years have passed since last update.

zshにgitのカレントブランチを常時表示させておく

Posted at

zsh

自分の環境で、gitのカレントブランチを確認するときに

$ git branch

を毎回叩くのって結構面倒なので、常に表示させるようにしています。
その方法は、git branchをすると、カレントブランチには*がつくので、それを目印にしてsedをしてます。
また、gitで管理していないディレクトリの場合は出力しないように/dev/nullに捨ててます

まとめたのが下記のコマンドです

$ git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'

これを、.zshrcのPS1 (またはPROMPT1)などに

PS1="\$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/')"
PS1="\$(git status -s 2> /dev/null)" # git status -sを出力

のように \$() で括るとコマンド実行時に解析するようになります。

くわしくはkaneshin/dotfiles

  • .zshrc
  • .shrc.common

をみてもらったほうがいいですね。zshrcの中身はエスケープ文字使ってるんで、気をつけてください。

12
12
2

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
12
12