Skip to content

Instantly share code, notes, and snippets.

@runejuhl
Created February 14, 2016 03:24
Show Gist options
  • Save runejuhl/6a71bb9a479d4e5f8093 to your computer and use it in GitHub Desktop.
Save runejuhl/6a71bb9a479d4e5f8093 to your computer and use it in GitHub Desktop.
Git info in $PS1
# Useful for displaying git status whenever you cd to a git dir.
# Example:
# runejuhl@cartagena:~//git/project:master:2A1AM5M3??
# for 2 new staged files, 1 staged file that's been modified since staging, 5 modified files and 3 untracked files.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/:\1/'
}
parse_git_status() {
git -c color.status=always status -s 2> /dev/null|sed -r 's/[ ]?(\x1b\[[0-9]{2}m)([^ ]+).*/\1\2\x1b\[m/'|sort -r|uniq -c|sed -r 's/[[:blank:]]+([0-9]+) (.*)/\1\2/'|tr -d '\n'|sed -r 's/(.*)/:\1/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;33m\]$(parse_git_branch)\033[m$(parse_git_status)\[\033[00m\]\n\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment