skip to content

Pretty Colored Elided Shell Prompt

This morning, @0xMF’s tweet prompted me to finally pimp my shell prompt.

I used to use the following blunt $PS1:

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

No color and rather uncomfortable when navigating deep inside directory hierarchies.

It’s in fact very easy to change that into a pretty colored & elided prompt:

Pretty Colored Elided Shell Prompt

Just copy the following lines into your .bash_profile or .bashrc:

PS1_PWD_MAX=15
__pwd_ps1() { echo -n "$PWD" | sed -e "s|${HOME}|~|" -e "s|\(/[^/]*/\).*\(/.\{${PS1_PWD_MAX}\}\)|\1...\2|"; }

GIT_PS1_SHOWDIRTYSTATE=1
PS1='\[\033[01;34m\]$(__pwd_ps1)$(__git_ps1 " \[\033[01;31m\](%s)")$\[\033[00m\] '

Later on, if you feel like you need to see more of the current path, you can assign PS1_PWD_MAX a larger value without sourcing your .bash_profile or .bashrc again. Just do:

$ PS1_PWD_MAX=100

Beware GIT_PS1_SHOWDIRTYSTATE may sometimes feel slow despite being practical.