Adding Space Before Equals in Git Prompt

I like using Git-Prompt to decorate my bash prompt. It's nice, simple, and it would be perfect if it didn't put equals next to branch name. Not sure what I'm talking about?

If you have modified files, Git-Prompt will make sure to let you know by writing master %= in prompt line. If you want to quickly copy branch name, you simply double click it and only master is selected. However, when there are no modifications to Git tree, output will be master=. Notice the lack of space. When you double click on this, both branch name and equals sign get copied. What I want is to have a space no matter whether branch is modified or not.

Cause for this issue can be found in the following line

.git-prompt.sh
local gitstring="$c$b${f:+$z$f}$r$p"

This makes it for that extra flags get space. If there are no flags, there is no space. Solution is simple - just add space always thus changing line like this:

.git-prompt.sh
local gitstring="$c$b$z$f$r$p"

Or, if you like to do it programmatically:

Terminal
sed -i 's/local gitstring=.*/local gitstring=$c$b$z$f$r$p/' ~/.git-prompt.sh

PS: If you are wondering, this is how my prompt setup looks:

~/.bash_profile
LC_COLLATE=C

if [ -f ~/.git-prompt.sh ]; then
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_STATESEPARATOR=" "
GIT_PS1_DESCRIBE_STYLE="default"
GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_HIDE_IF_PWD_IGNORED=
. ~/.git-prompt.sh
fi

function ps1_timer_start {
PS1_TIMER=${PS1_TIMER:-$SECONDS}
}

function ps1_timer_stop {
PS1_TIMER_VALUE=$(($SECONDS-$PS1_TIMER))
if [[ $PS1_TIMER_VALUE -eq 0 ]]; then
PS1_TIMER_VALUE=""
elif [[ $PS1_TIMER_VALUE -lt 60 ]]; then
PS1_TIMER_VALUE=" ${PS1_TIMER_VALUE}s"
else
PS1_TIMER_VALUE=" $((PS1_TIMER_VALUE / 60))m$((PS1_TIMER_VALUE % 60))s"
fi
unset PS1_TIMER
}

trap 'ps1_timer_start' DEBUG
PROMPT_COMMAND=ps1_timer_stop

PS1='\[\e[36m\]\n\u@\h\[\e[0m\] \w\[\e[34m\]$PS1_TIMER_VALUE\[\e[37m\] \[\e[36m\]\[\e[7m\]`__git_ps1 " %s "`\[\e[0m\]\n\[\e[36m\]\\$\[\e[0m\] '

Leave a Reply

Your email address will not be published. Required fields are marked *