Posted by mikhailian on Wed 12 Sep 2007 at 10:22
Debian shows the absolute path in the command prompt by default, and it can be really long, sometimes. This can take up valuable space in your shell windows.
To fix this, we can limit the command prompt to show only the last x characters using only the variable expansion features of bash 2.0.5+.
Just put the following snippet of code into ~/.bashrc
function truncate_pwd
{
if [ $HOME == $PWD ]
then
newPWD="~"
elif [ $HOME == ${PWD:0:${#HOME}} ]
then
newPWD="~${PWD:${#HOME}}"
else
newPWD=$PWD
fi
local pwdmaxlen=15
if [ ${#newPWD} -gt $pwdmaxlen ]
then
local pwdoffset=$(( ${#newPWD} - $pwdmaxlen ))
newPWD=".+${newPWD:$pwdoffset:$pwdmaxlen}"
fi
}
PROMPT_COMMAND=truncate_pwd
PS1="${ttyname}@\[${HOST_COLOUR}\]\h\[${RESET_COLOR}\]:\${newPWD}\\$ "
Since this code does not fork out sed, tr or wc, it is blazingly fast.
This article can be found online at the Debian Administration website at the following bookmarkable URL (along with associated comments):
This article is copyright 2007 mikhailian - please ask for permission to republish or translate.