Skip to content

Instantly share code, notes, and snippets.

@robeden
Created April 30, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robeden/63198def4ca041af89e4f1d42d525be7 to your computer and use it in GitHub Desktop.
Save robeden/63198def4ca041af89e4f1d42d525be7 to your computer and use it in GitHub Desktop.
Bash profile for prompt with shortened working dir and git repo/branch
# This sets the bash prompt to:
# <user>@<host> <working_dir>[(<git_repo>=><git_branch)] $
# ------------- ------------- -------------------------
# Default Green Yellow
# Color
#
# Examples:
#
# - reden@reden-pc ~/ $
# - reden@reden-pc .../ttc/cluster/(cluster-service=>master) $
# Color guide:
# Green - 32
# Yellow - 33
parse_git_branch() {
ROOT=`git rev-parse --show-toplevel 2> /dev/null | awk -F "/" '{print $NF}'`
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ -n "$ROOT" ]; then
echo " ($ROOT => $BRANCH)"
fi
}
generate_pwd() {
pwd | sed s.$HOME.~.g | awk -F"/" '
BEGIN { ORS="/" }
END {
SHORTENING=0
for (i=1; i<= NF; i++) {
if (i == NF-1 || i == NF) {
# Uncomment this to print both first two and last two directories
# if ((i == 1 && $1 != "") || (i == 2 && $2 != "") || i == NF-1 || i == NF) {
print $i
}
else if (i == 1 && $1 == "") {
print "/"$2
i++
}
else if (SHORTENING == 0) {
print "..."
SHORTENING=1
}
}
}'
}
# export PS1="\u@\h \[\033[32m\]\$(generate_pwd)\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\n$ "
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\[\033[1m\]\$(parse_git_branch)\[\033[00m\]\n$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment