2

I'd like to add git branch name to the terminal prompt string.

user@machine_name ~/workingdirectory (git_branch_name) $

I've tried pasting this code into the bashrc file:

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

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\>>>>[\033[00m\] $ "

However, this code overrides pantheon-terminal's default styling, which I want to keep.

Is there a way I can add the git branch name to the prompt string without overriding the default pantheon-terminal prompt string styling?

2 Answers 2

1

I'd suggest something like this add function like you are doing (yours didn't work for me didn't have time to debug):

function parse_git_branch() {
   git branch 2> /dev/null | sed --quiet 's/* \(.*\)/(\1) /p'
}

Then

export PS1=$PS1'$(parse_git_branch)'

If you need to do a more complex substitution then you might need to use sed or similar on the defaut eOS PS1 command, but if you replace it entirely you'll need to maybe look at what it does first, to find out out how to get similar functionality with custom behavior.

2
  • The default is \[\e]0;$(print_title)\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ for reference Aug 10, 2017 at 10:49
  • Finally, it may not be very eOS, but I use oh-my-zsh with the git plugin enabled, and I love that shell too much to move! Aug 10, 2017 at 10:51
1

I'm using these lines in my .bashrc file:

#if [ "$color_prompt" = yes ]; then
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
#fi
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

and it seems to have pretty much the same style as the default elementary OS bash prompt. As you can see, I have commented out the default PS1 setting so it is easier to revert the change. The important part is to put the new PS1 setting lines before the

unset color_prompt force_color_prompt

line. Otherwise the prompt will not have colors.

source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.