All 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Originally posted by bubulle on Planet Debian, a shell prompt that displays the current git branch, in colour on some terminals, after the current working directory. The following snippet does similar things for mksh users, except it doesn’t redefine your prompt but amend it – just throw it at the bottom of your ~/.mkshrc before that last line beginning with a colon (copy from /etc/skel/.mkshrc if you haven’t done that yet):
function parse_git_branch { git branch 2>/dev/null | sed -n '/^\* \(.*\)/s//(\1)/p' } function amend_prompt_with_git { local p q='$(parse_git_branch)' r if [[ $TERM = @(xterm-color|xterm|screen*) ]]; then if [[ ${PS1:1:1} = $'\r' ]]; then p=${PS1:0:1} else p=$'\001' PS1=$p$'\r'$PS1 fi q=$p$'\e[1;33m'$p$q$p$'\e[0m'$p fi p=${PS1%%*( )[#$]*( )} if [[ $p != "$PS1" ]]; then # prompt ends with space + #-or-$ + space, we can amend r=${PS1: ${#p}} PS1=$p$q$r fi } amend_prompt_with_git unset -f amend_prompt_with_git
The indirection by use of a function is not strictly necessary but allows the use of locals. I took the liberty of adding an asterisk after “screen” to match the GNU/Linux nonsense of having TERM=screen.xterm or somesuch.
