codetemplar
codetemplar

Reputation: 681

Adding a functional call to my PS1 command in bashrc

I have added the following function to .bashrc to show which git branch I am on in the terminal

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

I know I need to call it like so:

PS1="\h:\W \u$BLUE\$(parse_git_branch) $DEFAULT\$"

but my current .bashrc already has a stirng assigned to PS1, in multiple places. How do I update these strings so I keep my current colour formatting whilst calling this additional function?

This is what the section of the file assigning PS1 currently looks like:

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
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

Thanks

Upvotes: 0

Views: 330

Answers (1)

Barmar
Barmar

Reputation: 782529

Insert the function call into the existing prompts.

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] $BLUE$(parse_git_branch) $DEFAULT\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $(parse_git_branch) \$ '
fi

The color prompt also includes the $BLUE and $DEFAULT variables to change color, the non-color prompt just has the function call.

Upvotes: 1

Related Questions