Skip to content

Instantly share code, notes, and snippets.

@elia
Created October 14, 2009 10:18
Show Gist options
  • Save elia/209956 to your computer and use it in GitHub Desktop.
Save elia/209956 to your computer and use it in GitHub Desktop.
A minimalistic-colored-git-enabled bash prompt: gitdir ? "myproject:mybranch> " : "> "
#!/usr/bin/env bash
# coding: utf-8
# PROMPT
# more compact
# export PS1="\u@\h\w$ "
# export PS1=">: " # LOST style
# export PS1='> ' # Terminal Icon style
# Colored Terminal Icon style, with git project and branch when in git dir
# the $'...' solution prevents readline from messing up when walkin history:
# ( See:
# http://forums.macosxhints.com/showthread.php?t=90387
#
# $'...' instead of "...", to force \e to expand to ESC. It's a bash option
# whether that expansion happens inside "...", and it's usually safer if it
# doesn't, to guard against filenames containing escape sequences."
# )
red=$'\[\e[31m\]'
green=$'\[\e[32m\]'
yellow=$'\[\e[33m\]'
clear=$'\[\e[0m\]'
function git_branch {
if gitdir=`git show-git-dir` && test -f "$gitdir/.git/HEAD"
then
git_head=`cat $gitdir/.git/HEAD`
git_head="${git_head#ref\: refs\/heads\/}"
git_sha=`git rev-parse --short $git_head`
match=`expr $git_head : $git_sha`
# If git_ref contains git_sha then use sha which is shorter
if test $match = '0'
then
echo -n ":"$git_head
else
echo -n ":"$git_sha
fi
fi
}
function git_dir {
gitdir=`git show-git-dir` && test -f "$gitdir/.git/HEAD" && basename "$gitdir"
}
export PS1="$red\$(git_dir)$green\$(git_branch)$yellow>$clear "
# To include this custom prompt put this file in "~/bin/" and and put the
# following lines in "~/.profile".
#
# # Include Custom prompt
# . ~/bin/custom-prompt.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment