Skip to content

Instantly share code, notes, and snippets.

@nefigah
Created June 18, 2014 17:08
Show Gist options
  • Save nefigah/b95b4cc623564c7513cd to your computer and use it in GitHub Desktop.
Save nefigah/b95b4cc623564c7513cd to your computer and use it in GitHub Desktop.
### Prompt customization ###
COLOR_ABBR_PS () {
local str_max_length=3
local norm_color='\[\033[33m\]'
local abbr_color='\[\033[1;32m\]'
local user_color='\[\033[1;34m\]'
local no_color='\[\033[0m\]'
local dir_sep="$norm_color/"
local path="${PWD/$HOME}"
local curwd nextdir dirnames
[ "$path" != "$PWD" ] && curwd="$curwd$norm_color~"
local i=0
until [ "$path" == "$nextdir" ]; do
path=${path#*/}
nextdir=${path%%/*}
dirnames[i]=$nextdir
(( i += 1 ))
done
local lastdir=0
[ "${#dirnames[@]}" -ne 0 ] && lastdir="$((${#dirnames[@]} - 1))"
for i in $(seq 0 $lastdir); do
if [ "$i" -eq $lastdir ]; then
curwd="$curwd$dir_sep${dirnames[$i]}"
elif [ "$i" -eq 0 ] || [ $((i+1)) -ge $lastdir ]; then
if [ "${#dirnames[$i]}" -le "$str_max_length" ]; then
curwd="$curwd$dir_sep${dirnames[$i]}"
else
curwd="$curwd$dir_sep$abbr_color${dirnames[$i]:0:$str_max_length}"
fi
else
curwd="$curwd$dir_sep$abbr_color${dirnames[$i]:0:1}"
fi
done
if [ -z "$abbr_host" ]; then
abbr_host=$(echo "${HOSTNAME:-nil}" | sed 's/\..*$//')
abbr_pat='[-_ ]'
# Regex matching is even more finnicky than the rest of bash. Don't quote pat in [[ expr ]]
[[ "$abbr_host" =~ $abbr_pat ]] && abbr_host=$(echo "$abbr_host" | sed 's/\([[:alnum:]]\)[[:alnum:]]*/\1/g')
fi
PS1="[$user_color\u@$abbr_host $curwd$no_color]$user_color\$ $no_color"
}
PROMPT_COMMAND="COLOR_ABBR_PS; $PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment