Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Last active April 7, 2021 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaymecd/6887829 to your computer and use it in GitHub Desktop.
Save jaymecd/6887829 to your computer and use it in GitHub Desktop.
PS1 bash prompt with git info
case $( uname -s ) in
Darwin)
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
;;
Linux)
alias ls="ls --color"
;;
esac
alias grep="egrep --color"
alias egrep="egrep --color"
alias fgrep="fgrep --color"
alias ll="ls -lph"
alias lla="ll -A"
alias less="less -R"
alias cdiff="colordiff | less"
alias rm="rm -i"
alias cp="cp -i"
alias mv="mv -i"
#
# PS prompts
# file: /etc/bash_prompt
function __ps_git() {
if ! declare -f -F __git_ps1 >/dev/null; then
return
fi
local state="$(__git_ps1 ' (git %s)')"
if [[ ! -z $state ]]; then
local hash=$(git rev-parse --verify --short HEAD 2>/dev/null)
state=$(echo "$state" | sed "s/)/ $hash)/" | sed "s/\s*)/)/")
fi
echo "$state"
}
function __ps_svn() {
local path name rev
if svn info >/dev/null 2>&1; then
path="$(svn info | grep '^URL:' | egrep -o '(tags|branches)/[^/]+|trunk' | sed -e 's|^tags/\(.\+\)$|(\1)|')"
rev="$(svn info | awk '/Revision:/ {print $2}')"
if [ -z "$path" ]; then
path="$(svn info | grep '^Repository Root:')"
fi
name="$(echo $path | egrep -o '[^/]+$')"
fi
if [ ! -z "$name" ]; then
echo -n " (svn $name $rev)"
fi
}
function __ps_aws() {
[ $AWS_CONFIG_FILE ] && echo " (aws ${AWS_DEFAULT_PROFILE:-default})"
}
function __ps1_prompt() {
local x="\[\e[0m\]"
local b="\[\e[0;30m\]"
local r="\[\e[0;31m\]"
local g="\[\e[0;32m\]"
local gr="\[\e[1;30m\]"
local y="\[\e[0;33m\]"
local bl="\[\e[1;34m\]"
local p="\[\e[0;35m\]"
local c="\[\e[0;36m\]"
local w="\[\e[0;37m\]"
if [ $(id -u) = 0 ]; then
echo "${r}\u ${w}@ ${y}\h ${bl}\w${y}\$(__ps_git)\$(__ps_svn)${gr}\$(__ps_aws)${x}\n${c}[\t]${x} ${r}\$${x} "
else
echo "${g}\u ${w}@ ${y}\h ${bl}\w${y}\$(__ps_git)\$(__ps_svn)${gr}\$(__ps_aws)${x}\n${c}[\t]${x} ${g}\$${x} "
fi
}

Dynamic bash prompt and alias list

$ curl -sSL https://gist.github.com/jaymecd/6887829/raw/bash_prompt | sudo tee /etc/bash_prompt

$ curl -sSL https://gist.github.com/jaymecd/6887829/raw/bash_alias | sudo tee /etc/bash_alias

$ curl -sSL https://gist.github.com/jaymecd/6887829/raw/bashrc | tee -a ~/.bashrc
[ -f /etc/bash_completion ] && source /etc/bash_completion
[ -f /etc/bash_prompt ] && source /etc/bash_prompt
[ -f /etc/bash_alias ] && source /etc/bash_alias
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
if declare -f -F __ps1_prompt >/dev/null; then
export PS1="$(__ps1_prompt)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment