Skip to content

Instantly share code, notes, and snippets.

@max-lt
Last active October 4, 2018 11:07
Show Gist options
  • Save max-lt/5c146f604594b82080f7051840be17b5 to your computer and use it in GitHub Desktop.
Save max-lt/5c146f604594b82080f7051840be17b5 to your computer and use it in GitHub Desktop.
Long lines shortening in Bash PS1 customized prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
parse_path() {
if [ / = $PWD ]; then
echo "/";
return;
fi
local path=`dirs`;
local len=${#path};
if [ $len -lt 20 ]; then
echo "$path/";
return;
fi
# https://muffinresearch.co.uk/showing-last-two-directories-of-pwd-in-bash-prompt/
local path=`pwd | awk -F\/ '{print $(NF-1),$(NF)}' | sed "s/ /\\//"`
echo ".../$path/";
}
# User in bold yellow
PS1="\[\033[01;33m\]\u\[\033[01;0m\]"
# Hostname
PS1=$PS1"@\h"
# Full path in bold cyan (we use a function to get pwd and shorten it instead of "\w")
PS1=$PS1"\[\033[01;36m\] \$(parse_path)\[\033[01;0m\]"
# Git branch in yellow (if any)
PS1=$PS1"\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment