Skip to content

Instantly share code, notes, and snippets.

@mkottman
Last active July 19, 2023 06:53
Show Gist options
  • Save mkottman/1936195 to your computer and use it in GitHub Desktop.
Save mkottman/1936195 to your computer and use it in GitHub Desktop.
A two-line colored Bash prompt (PS1) with Git branch and a line decoration which adjusts automatically to the width of the terminal. Recognizes SVN, Git and Fossil version control systems and shows the current branch/revision.
# A two-line colored Bash prompt (PS1) with Git branch and a line decoration
# which adjusts automatically to the width of the terminal.
# Recognizes and shows Git, SVN and Fossil branch/revision.
# Screenshot: http://img194.imageshack.us/img194/2154/twolineprompt.png
# Michal Kottman, 2012
RESET="\[\033[0m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\033[0;33m\]"
PS_LINE=`printf -- '- %.0s' {1..200}`
function parse_git_branch {
PS_BRANCH=''
PS_FILL=${PS_LINE:0:$COLUMNS}
if [ -d .svn ]; then
PS_BRANCH="(svn r$(svn info|awk '/Revision/{print $2}'))"
return
elif [ -f _FOSSIL_ -o -f .fslckout ]; then
PS_BRANCH="(fossil $(fossil status|awk '/tags/{print $2}')) "
return
fi
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
PS_BRANCH="(git ${ref#refs/heads/}) "
}
PROMPT_COMMAND=parse_git_branch
PS_INFO="$GREEN\u@\h$RESET:$BLUE\w"
PS_GIT="$YELLOW\$PS_BRANCH"
PS_TIME="\[\033[\$((COLUMNS-10))G\] $RED[\t]"
export PS1="\${PS_FILL}\[\033[0G\]${PS_INFO} ${PS_GIT}${PS_TIME}\n${RESET}\$ "
Copy link

ghost commented May 11, 2016

Hello,

  1. Thanks for this great prompt!
  2. You can add the following to support also Mercurial:
    23 elif [ -d .hg ]; then
    24 PS_BRANCH="(hg $(hg branch))"
    25 return
    26 fi
  3. I would rename PS_GIT to PS_VCS (Version Control System) which makes more sense ;)

KR
Sam

@edupo
Copy link

edupo commented Jun 9, 2016

Hey, thanks! This is what I was looking for... Forked!
I add changes suggested by @DrumMeister and my own.

@incrazyboyy
Copy link

The screenshot link is down, would love to see it before applying!

@osipxd
Copy link

osipxd commented Aug 1, 2017

@incrazyboyy
In Deepin Terminal with pali theme and default Terminal with dark theme:
image

@zaharcelac
Copy link

To have correct # vs $ while sudo -s, in the last line we had to use single quotes around $:
export PS1="\${PS_FILL}\[\033[0G\]${PS_INFO} ${PS_GIT}${PS_TIME}\n${RESET}"'\$ '

@juhp
Copy link

juhp commented Oct 26, 2018

or one can just quote the backslash before the last $: "...\n${RESET}\\$ "

@juhp
Copy link

juhp commented Nov 2, 2018

@mem-
Copy link

mem- commented Jul 15, 2022

Ho handle git unborn repo (newly created) and git detached HEAD, I replaced lines 24 and 25 with;

    branches=$(git branch 2> /dev/null) || return
    ref=$(echo "$branches" | grep "^*" | sed -e 's/^* //')
    [ "x${ref}" = "x" ] && ref="unborn: $(git symbolic-ref HEAD | sed -e 's#refs/heads/##')"
    PS_BRANCH="[git: ${ref}] "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment