Skip to content

Instantly share code, notes, and snippets.

@AysadKozanoglu
Last active October 28, 2020 09:34
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 AysadKozanoglu/f6b3b40c7dbc0d6c8cf93dabe3337d03 to your computer and use it in GitHub Desktop.
Save AysadKozanoglu/f6b3b40c7dbc0d6c8cf93dabe3337d03 to your computer and use it in GitHub Desktop.
bashrc cli template
# Shorten home dir, cygwin drives, paths that are too long
# (just bourne; no perl, sed, or bashisms!)
HOST='\033[02;36m\]\h'; HOST=' '$HOST
TIME='\033[01;31m\]\t \033[01;32m\]'
LOCATION=' \033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`'
BRANCH=' \033[00;33m\]$(git_branch)\[\033[00m\]\n\$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='\[\033[01;36m\]>'
function PSWD() {
local p="$*" parta partb
if [ "$p" = "${HOME:-empty}${p#$HOME}" ]
then p="~${p#$HOME}"
fi
# Fix Cygwin drive designations
if [ "$OS" != "${OS#*CYGWIN}" -a "${p#/cygdrive}" != "$p" ]; then
p="${p#/cygdrive/}"
parta="${p%%/*}"
if [ -z "$parta" ]
then p="$p:"
else p="$parta:${p#?}"
fi
fi
# if the resulting path is 34+ characters, truncate it
parta="${p#??????????????????????????????????}"
if [ "${parta:-$p}" != "$p" ]; then
parta="${p#??????????}" # the path, minus the first 10 chars
parta="${p%$parta}" # the first 10 chars of the path
partb="${p%????????????????????}" # the path, minus the last 20 chars
partb="${p#$partb}" # the last 20 chars of the path
p="$parta...$partb" # 10 chars plus 3 dots plus 20 chars = 33
fi
echo "$p"
}
PSC() { echo -ne "\[\033[${1:-0;38}m\]"; }
PR="0;32" # default color used in prompt is green
if [ "$(id -u)" = 0 ]; then
sudo=41 # root is red background
elif [ "$USER" != "${SUDO_USER:-$USER}" ]; then
sudo=31 # not root, not self: red text
else sudo="$PR" # standard user color
fi
PROMPT_COMMAND='[ $? = 0 ] && PS1=${PS1[1]} || PS1=${PS1[2]}'
PSbase="$(PSC $sudo)\u$(PSC $PR)@\h $(PSC 33)\$(PSWD \w)"
PS1[1]="$PSbase$(PSC $PR)\$ $(PSC)"
PS1[2]="$PSbase$(PSC 31)\$ $(PSC)"
PS1="${PS1[1]}"
unset sudo PR PSbase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment