Skip to content

Instantly share code, notes, and snippets.

@jeffsharpe
Last active July 7, 2017 19:47
Show Gist options
  • Save jeffsharpe/0e07dd9619f104f616d6 to your computer and use it in GitHub Desktop.
Save jeffsharpe/0e07dd9619f104f616d6 to your computer and use it in GitHub Desktop.
My ~/.bash_prompt script. 20170707
#
# ~/.bash_prompt
#
# Call this file from ~/.bashrc or ~/.bash_profile with
# source ~/.bash_prompt#
#
# Required Bash version 4.x or above
# Works on OSX, Linux and Cygwin
# Prints the git branch (if any)
function git_branch {
if which git &> /dev/null ; then
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
fi
}
# Colourizes the branch text based on status
function markup_git_branch {
local RED="\[\033[0;31m\]"
local YELLOW="\[\033[0;33m\]"
local NORMAL="\[\033[00m\]"
if [[ "x$1" = "x" ]]; then
#echo -e "[$1]"
echo ""
else
if [[ $(git status 2> /dev/null | tail -n1) = "nothing to commit, working tree clean" ]]; then
echo -e '\033[1;33m['"$1"']\033[0;0m'
else
echo -e '\033[1;31m['"$1"'*]\033[0;0m'
fi
fi
}
# Prompt
# Colours
# 30m - Black
# 31m - Red
# 32m - Green
# 33m - Yellow
# 34m - Blue
# 35m - Purple
# 36m - Cyan
# 37m - White
# 0 - Normal
# 1 - Bold
function bash_prompt {
local BLACK="\[\033[0;30m\]"
local BLACKBOLD="\[\033[1;30m\]"
local RED="\[\033[0;31m\]"
local REDBOLD="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local GREENBOLD="\[\033[1;32m\]"
local YELLOW="\[\033[0;33m\]"
local YELLOWBOLD="\[\033[1;33m\]"
local BLUE="\[\033[0;34m\]"
local BLUEBOLD="\[\033[1;34m\]"
local PURPLE="\[\033[0;35m\]"
local PURPLEBOLD="\[\033[1;35m\]"
local CYAN="\[\033[0;36m\]"
local CYANBOLD="\[\033[1;36m\]"
local WHITE="\[\033[0;37m\]"
local WHITEBOLD="\[\033[1;37m\]"
local NORMAL="\[\033[00m\]"
export PS1="${BLUE}[\D{%Y%m%d %k:%M}]${NORMAL} ${CYAN}[\!]${NORMAL} ${GREENBOLD}\u${NORMAL}@${GREENBOLD}\h${NORMAL}:${CYAN}\w${NORMAL} \$(markup_git_branch \$(git_branch)) \\$ "
# PS1='\u@\h:\w\$ '
if [ "$SSH_CONNECTION" ]; then
export PS1="${REDBOLD}(remote)${NORMAL} ${BLUE}[\D{%Y%m%d %k:%M}]${NORMAL} ${CYAN}[\!]${NORMAL} ${GREENBOLD}\u${NORMAL}@${GREENBOLD}\h${NORMAL}:${CYAN}\w${NORMAL} \\$ "
fi
}
bash_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment