I added a function to my .bashrc
to parse the git branch I'm currently working in and display it in my bash-prompt according to several tutorials on this topic.
My code snippet looks like this:
parse_git_branch() {
BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$BRANCH" != "" ]; then
echo -e "[\e[1;31m⎇ $BRANCH\e[1;37m]"
else
echo "╼"
fi
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[1;37m\]┌[\[\033[1;32m\]\u\[\033[1;37m\]@\[\033[1;32m\]\H\[\033[1;37m\]] [\w]\n└$(parse_git_branch) \[\033[00m\]'
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
This does as expected print the current git branch in red if there is one: screenshot
However, if the command is really long, the line doesn't break anymore: another screenshot
The problem goes away if I don't use the -e
flag for echo
in parse_git_branch
, but I need it to color the branch red.
Any ideas on how to fix this?
All the other solutions I found while googling this just print either the colored git branch or nothing, so they don't quite work for me since i want it to print the git branch if there is one and if not print that ╼
-character in white.