Skip to content

Instantly share code, notes, and snippets.

@lebensterben
Created July 5, 2019 23:17
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 lebensterben/f3401a701dacac991590455bc09b9e25 to your computer and use it in GitHub Desktop.
Save lebensterben/f3401a701dacac991590455bc09b9e25 to your computer and use it in GitHub Desktop.
# Set prompt and title (for interactive shells only)
if [ "$(expr $- : '.*i')" -ne 0 ]; then
# this works for sh and bash
if [ -z "$ZSH_VERSION" ]; then
# first get exit code of last command, and set colors
PS1="\$(\
EXIT=\"\$?\" ; \
BLUE=\"\[\e[38;5;39m\]\" ; \
RED=\"\[\e[31m\]\" ; \
ORANGE=\"\[\e[38;5;208m\]\" ; \
WHITE=\"\[\e[0m\]\" ; "
# endchar
# use # for root and $ for non-root users
# use white for default color, and red if last exit code is non-zero
PS1+="\
endchar=\"\\$\${WHITE}\" ; \
if [ \"\$EXIT\" -eq 0 ]; then
endchar=\"\${WHITE}\$endchar\" ; \
else \
endchar=\"\${RED}\$endchar\" ; \
fi ; "
# current directory in blue(39)
PS1+="\
dir=\"\${BLUE}\w\${WHITE}\" ; "
# check for $SSH_CONNECTION
# in case $SSH_CONNECTION is lost, detect it manually via `who`
_ssh_connection=${SSH_CONNECTION:-$__prompt_ssh_connection}
if [ -z "$_ssh_connection" ]; then
reIPv4='\(\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9]\{1,2\}\.\)\{3\}\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9]\{1,2\}\)\)'
reIPv6='\(\([0-9a-fA-F]\{1,4\}:\)\{7,7\}[0-9a-fA-F]\{1,4\}\|\([0-9a-fA-F]\{1,4\}:\)\{1,7\}:\|\([0-9a-fA-F]\{1,4\}:\)\{1,6\}:[0-9a-fA-F]\{1,4\}\|\([0-9a-fA-F]\{1,4\}:\)\{1,5\}\(:[0-9a-fA-F]\{1,4\}\)\{1,2\}\|\([0-9a-fA-F]\{1,4\}:\)\{1,4\}\(:[0-9a-fA-F]\{1,4\}\)\{1,3\}\|\([0-9a-fA-F]\{1,4\}:\)\{1,3\}\(:[0-9a-fA-F]\{1,4\}\)\{1,4\}\|\([0-9a-fA-F]\{1,4\}:\)\{1,2\}\(:[0-9a-fA-F]\{1,4\}\)\{1,5\}\|[0-9a-fA-F]\{1,4\}:\(\(:[0-9a-fA-F]\{1,4\}\)\{1,6\}\)\|:\(\(:[0-9a-fA-F]\{1,4\}\)\{1,7\}\|:\)\|fe80:\(:[0-9a-fA-F]\{0,4\}\)\{0,4\}%[0-9a-zA-Z]\{1,\}\|::\(ffff\(:0\{1,4\}\)\{0,1\}:\)\{0,1\}\(\(25[0-5]\|\(2[0-4]\|1\{0,1\}[0-9]\)\{0,1\}[0-9]\)\.\)\{3,3\}\(25[0-5]\|\(2[0-4]\|1\{0,1\}[0-9]\)\{0,1\}[0-9]\)\|\([0-9a-fA-F]\{1,4\}:\)\{1,4\}:\(\(25[0-5]\|\(2[0-4]\|1\{0,1\}[0-9]\)\{0,1\}[0-9]\)\.\)\{3,3\}\(25[0-5]\|\(2[0-4]\|1\{0,1\}[0-9]\)\{0,1\}[0-9]\)\)'
reHostname='\(\(\([a-zA-Z0-9]\|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\)\.\)*\([A-Za-z0-9]\|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]\)\)'
_ssh_connection="$(expr "$(who -m 2>/dev/null)" : '.* (\?\('$reIPv4'\|'$reIPv6'\|'$reHostname'\))\?' )"
fi
# export it so it works for programs such as tmux
export __prompt_ssh_connection="$_ssh_connection"
unset _ssh_connection
# hide hostname and username except for ssh or su sessions
# use red for root and blue(39) for non-root users
# hostname in orange
PS1+="\
if [ -n \"\$__prompt_ssh_connection\" ]; then
username=\"\${BLUE}\u\${WHITE}@\" ; \
if [ \$UID = 0 ]; then
username=\"\${RED}\u\${WHITE}@\" ; \
fi ; \
hostname=\"\${ORANGE}\H\${WHITE} \" ; \
else
if [ \"\$LOGNAME\" = \"\$(id -un)\" ]; then
: ; \
else
username=\"\${BLUE}\u\${WHITE} \" ; \
if [ \$UID = 0 ]; then
username=\"\${RED}\u\${WHITE} \" ; \
fi ; \
fi ; \
fi ; "
# set prompt, and additionally set window title for xterm
if [ "${TERM:0:5}" = "xterm" ]; then
PS1+="echo \"\[\e]2;\u@\H :: \w\a\]\$username\$hostname\$dir \$endchar \")"
else
PS1+="echo \"\$username\$hostname\$dir \$endchar\")"
fi
else
# this works for zsh
# endchar
# use red if last command has non-zero exit
# use # for root and $ for non-root users
local _root_endch="%(?.#.%F{red}#%f)"
local _other_endch="%(?.$.%F{red}$%f)"
local _endchar="%(#.${_root_endch}.${_other_endch})"
# current directory in blue(39)
local _dir="%F{39}%~%f"
# check for $SSH_CONNECTION
# in case $SSH_CONNECTION is lost, detect it manually via `who`
local _ssh_connection="${SSH_CONNECTION:-$__prompt_ssh_connection}"
if [ -z "$_ssh_connection" ]; then
local _reIPv4='([0-9]{1,3}\.){3}[0-9]+'
local _reIPv6='(([0-9a-fA-F]+:)|:){2,}[0-9a-fA-F]+'
local _reHostname='([.][^. ]+){2}'
local -H MATCH MBEGIN MEND
local -a match mbegin mend
if [[ "$(who -m 2>/dev/null)" =~ "(\(?)($_reIPv4|$_reIPv6|$_reHostname)(\)?\$)" ]]; then
_ssh_connection=${match[2]}
# export it so it works for programs such as tmux
export __prompt_ssh_connection="$_ssh_connection"
fi
fi
# hide hostname and username except for ssh or su sessions
if [ -n "$_ssh_connection" ]; then
# use red for root and blue(39) for non-root users
local _username="%F{%(#.red.39)}%n%f@"
# hostname in orange
local _hostname="%F{208}%m%f "
# check if current session is in `su`
elif [ "$LOGNAME" != "$(id -un)" ]; then
# use red for root and blue(39) for non-root users
local _username="%F{%(#.red.39)}%n%f "
fi
# set prompt
PS1="${_username}${_hostname}${_dir} ${_endchar} "
# additionally set window title for xterm
__clr_default_title () { # for xterm, set window title
if [ "${TERM:0:5}" = "xterm" ]; then
print -Pn "\e]2;%n\@%m :: %~\a"
fi
}
__clr_default_title
autoload -Uz add-zsh-hook
add-zsh-hook chpwd __clr_default_title
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment