Skip to content

Instantly share code, notes, and snippets.

@theon
Last active October 19, 2015 09:25
Show Gist options
  • Save theon/3895883 to your computer and use it in GitHub Desktop.
Save theon/3895883 to your computer and use it in GitHub Desktop.
My bash prompt
RESET="\[\017\]"
NORMAL="\[\033[0m\]"
YELLOW="\[\033[33;1m\]"
function colour256() {
echo "\[\033[38\;5\;$1m\]";
}
# Alternative to \W. \W works in $PS1, but bash can not interpret it
function pwdbn() {
if [ "`pwd`" == "$HOME" ]; then echo "~"; else basename "`pwd`"; fi
}
# Function to generate prompt.
# Params are:
# $1 = Successful prompt colour
# $2 = Unsuccessul prompt colour
# $3 = GIT branch colour
# Don't specify params for plain no colour prompt
function prompt() {
# Must be first line in function to detect if last command was successful
if [ $? = 0 ]; then COLOUR_BN=$1; else COLOUR_BN=$2; fi
BASENAME=`pwdbn`
git branch &>/dev/null
if [ $? = 0 ]; then
BRANCH=" (`git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null`)"
fi
echo "${COLOUR_BN}${BASENAME}${3}${BRANCH}"
}
function prompt_symbol() {
if [ "`whoami`" == "root" ]; then echo '#'; else echo '$'; fi
}
# Throw it all together
PS1=" ${RESET}\`prompt $(colour256 40) $(colour256 52) $NORMAL\` ${YELLOW}`prompt_symbol` ${NORMAL}"
PS2=" \`prompt | sed 's/./ /g'\` ${YELLOW}… ${NORMAL}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment