Skip to content

Instantly share code, notes, and snippets.

@kav2k
Last active May 14, 2019 15:04
Show Gist options
  • Save kav2k/33322315d87ebc0f27ea6944f6c2186e to your computer and use it in GitHub Desktop.
Save kav2k/33322315d87ebc0f27ea6944f6c2186e to your computer and use it in GitHub Desktop.
My personal Bash prompt template
# My personal bash prompt template
# (c) Alexander Kashev, 2017-2019
# To use, copy to home and add to .bashrc:
#
# if [ -f $HOME/.bash_prompt ]; then
# # For cluster machines: appends suffix if short hostname not equal to it
# PS1_CLUSTER_NAME=''
# . $HOME/.bash_prompt
# fi
getPS1_truncatePath() {
if [[ $1 == '/' ]] ; then
echo ""
else
echo $1 | sed s,^$HOME,~, | sed s,[^/]*$,,
fi
}
getPS1_hostColor() {
local Hostname=$1
if (( $(tput colors) > 255 )); then
echo "38;5;$(((((0x$(echo ${Hostname} | md5sum | cut -f 1 -d " ") % 215) + 215) % 215) + 17))m"
else
echo "$(((((0x$(echo ${Hostname} | md5sum | cut -f 1 -d " ") % 15) + 15) % 15) + 31))m"
fi
}
getPS1() {
# For cluster machines: appends suffix if short hostname not equal to it
local HostnameSuffix=$PS1_CLUSTER_NAME
# Normal Colors
local Black="\[\e[0;30m\]" # Black
local Red="\[\e[0;31m\]" # Red
local Green="\[\e[0;32m\]" # Green
local Yellow="\[\e[0;33m\]" # Yellow
local Blue="\[\e[0;34m\]" # Blue
local Purple="\[\e[0;35m\]" # Purple
local Cyan="\[\e[0;36m\]" # Cyan
local White="\[\e[0;37m\]" # White
# Bold
local BBlack="\[\e[1;30m\]" # Black
local BRed="\[\e[1;31m\]" # Red
local BGreen="\[\e[1;32m\]" # Green
local BYellow="\[\e[1;33m\]" # Yellow
local BBlue="\[\e[1;34m\]" # Blue
local BPurple="\[\e[1;35m\]" # Purple
local BCyan="\[\e[1;36m\]" # Cyan
local BWhite="\[\e[1;37m\]" # White
# Background
local On_Black="\[\e[40m\]" # Black
local On_Red="\[\e[41m\]" # Red
local On_Green="\[\e[42m\]" # Green
local On_Yellow="\[\e[43m\]" # Yellow
local On_Blue="\[\e[44m\]" # Blue
local On_Purple="\[\e[45m\]" # Purple
local On_Cyan="\[\e[46m\]" # Cyan
local On_White="\[\e[47m\]" # White
local NC="\[\e[m\]" # Color Reset
if [[ ${EUID} == 0 ]] ; then
local UserColor=$BRed
local PromptSymbol="${BWhite}#${NC}"
else
local UserColor=$BGreen
local PromptSymbol="${BWhite}\$${NC}"
fi
local User="${UserColor}\u${NC}"
if [[ -n $(type -t __git_ps1) ]] ; then
local GitPromptText='$(__git_ps1 " (%s)")'
local GitPrompt="${BYellow}${GitPromptText}${NC}"
else
local GitPrompt=""
fi
local TruncatedPath='$(getPS1_truncatePath \w)'
local HighlightedPath="${BBlue}${TruncatedPath}${BCyan}\W"
local HostColor="$(getPS1_hostColor $(hostname))"
if [[ ${HostnameSuffix} && $(hostname -s) != ${HostnameSuffix} ]] ; then
local SuffixColor="$(getPS1_hostColor ${HostnameSuffix})"
local AtHostname="\[\e[1;${HostColor}\]@\h\[\e[0;${SuffixColor}\].${HostnameSuffix}${NC}"
else
local AtHostname="\[\e[1;${HostColor}\]@\h${NC}"
fi
local WindowTitle="\u@\h:\w"
local WindowTitleUpdate="\[\e]2;${WindowTitle}\a\]"
local Prompt="${User}${AtHostname}:${HighlightedPath}${GitPrompt}\n${WindowTitleUpdate}${PromptSymbol} "
echo "$Prompt"
}
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWCOLORHINTS=1
PS1=$(getPS1)
# To merge history from different sessions, append on every command
export PROMPT_COMMAND="history -a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment