Skip to content

Instantly share code, notes, and snippets.

@michd
Last active August 29, 2015 14:01
Show Gist options
  • Save michd/7a1c62829a6de0fb7786 to your computer and use it in GitHub Desktop.
Save michd/7a1c62829a6de0fb7786 to your computer and use it in GitHub Desktop.
My bash prefix (PS1)
# Return current git branch prefixed with |, nothing if not in git repo
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\|\1/'
}
# Returns some "dirty" flags if they apply, nothing if not in git repo,
# or current branch is clean.
# Flags: M = modified files, ? = untracked files, % = unresolved merges
# These flags are prefixed with a |
parse_git_dirty() {
_gst_output=$(git status --porcelain 2> /dev/null)
_output=""
if [[ ! "$_gst_output" = "" ]]; then
if [[ ! $(git status --porcelain 2> /dev/null | grep ^\ M\ ) = "" ]]; then
_output="M"
fi
if [[ ! $(git status --porcelain 2> /dev/null | grep ^??) = "" ]]; then
_old_output=$_output
_output="?$_old_output"
fi
if [[ ! $(git status --porcelain 2> /dev/null | grep ^Unmerged\ paths:) = "" ]]; then
_old_output=$_output
_output="%$_old_output"
fi
echo "|$_output"
fi
}
#Format a nice command line prefix including current git branch
# Colors are ugly to do in bash.
# Output format = <user>@<host>:<path>[|<git branch>][|<git dirty flags>]
PS1="\[\033[01;36m\]\u\[\033[00m\]\[\033[01;30m\]@\h\[\033[00m\]:\[\033[00;37m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\033[00;31m\]\$(parse_git_dirty)\[\033[00m\] >> "
# non-colored version:
# PS1="\u@\h:\w|\$(parse_git_branch)\$(parse_git_dirty) >> "

Here's a screenshot of it on my computer, with untracked files and modified files.

PS1 screenshot

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