Controlling the size of the $PWD in bash
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.
PS1='[\u@\h \W]\\$ '
And when I'm using konsole, I also have this:
PROMPT_COMMAND='PWDF=`pwd|sed "s/.*\/\(.*\)/\1/"`; echo -ne "\033]0;$TTY:${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007\033] 30;${HOSTNAME%%.*}:${PWDF}\007"'
which does have sed in it so could probably be optimised in the way you describe.
(that changes the tab name to "machine:dir" and the window title to "tty:user@machine:pwd")
[ Parent | Reply to this comment ]
Updated it to get paths with "..." in the middle :
function truncate_pwd
{
if [ $HOME == $PWD ]
then
newPWD="~"
elif [ $HOME == ${PWD:0:${#HOME}} ]
then
newPWD="~${PWD:${#HOME}}"
else
newPWD=$PWD
fi
local pwdmaxlen=50
local pwdstartlen=20
local pwdendlen=$(( $pwdmaxlen - $pwdstartlen ))
if [ ${#newPWD} -gt $pwdmaxlen ]
then
local pwdoffset=$(( ${#newPWD} - $pwdendlen ))
newPWD="${newPWD:0:$pwdstartlen}...${newPWD:$pwdoffset:$pwde ndlen}"
fi
}
[ Parent | Reply to this comment ]
PS1='${debian_chroot:+($debian_chroot)}\u@\h:$(if test $(echo $PWD | sed "s,$HOME,~," | wc -c) -gt 40; then echo "${PWD: -40}" | sed 's,^[^/]*,...,'; else echo "\w"; fi)\$ '
[ Parent | Reply to this comment ]
[ Send Message | View dkg's Scratchpad | View Weblogs ]
[ Parent | Reply to this comment ]
function truncate_pwd
{
newPWD="${PWD/#$HOME/~}"
local pwdmaxlen=15
if [ ${#newPWD} -gt $pwdmaxlen ]
then
newPWD=".+${newPWD: -$pwdmaxlen}"
fi
}
PROMPT_COMMAND=truncate_pwd
PS1="${ttyname}@\h:\${newPWD}\\$ "
[ Parent | Reply to this comment ]
function truncate_pwd
{
newPWD="${PWD/#$HOME/~}"
local pwdmaxlen=$((${COLUMNS:-80}/3))
[ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...{newPWD:3-$pwdmaxlen}"
}
[ Parent | Reply to this comment ]
function truncate_pwd
{
newPWD="${PWD/#$HOME/~}"
local pwdmaxlen=$((${COLUMNS:-80}/3))
[ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...${newPWD:3-$pwdmaxlen}"
}
PROMPT_COMMAND="$PROMPT_COMMAND;"truncate_pwd
PS1=${PS1//\\w/\$\{newPWD\}}
PS: Nobody should learn to use a shell when he already knows a good programming language. Things look much cleaner in real programs. (hint: aptitude install nemerle)
[ Parent | Reply to this comment ]
PS1="%n@%m %$pwd_len<..<%~ "
the "%~" automatically simplifies "/home/user/path" to "~user/path"
the "<..<" replaces left (hence '<') part of pwd by ".." if it is longer than $pwd_len
("%m" for hostname, and "%n" for username)
(I personally add "%S%#%s" to mark the "true" prompt (the '#' character if root, '$' or '%' otherwise) with reverse colors)
it seems debian-administration's competence is cratering :(
[ Parent | Reply to this comment ]
Are you saying this because the article offered a version that was revised
later in the light of comments? Hence implying the article wasn't of
a sufficiently polished to merit release?
I don't expect articles to be necessarily written by someone who knows it all
inside-out. On the other hand, I don't expect something hopelessly clueless -
I do expect the writer to be someone who's thought about it and has had the
piece accepted by a reasonably competent moderator. I thought this article was
ok in these respects. If you found the level too low, it's probably because
without realising it, you've become pretty good at this sort of thing yourself.
Plus I find the comments that come in are often a valuable analysis on most
articles, (which are of a pretty good standard to begin with).
Indeed, your zsh comment is itself a classic example of this, being a
contribution in raising awareness of the zsh alternative to bash.
PJ
[ Parent | Reply to this comment ]
(btw excuse me if the author felt offended)
[ Parent | Reply to this comment ]
I think most people agree that the default prompt isn't good, but it's hard to agree what the perfect prompt is?
See the following books:
* Automating UNIX and Linux Administration
http://www.apress.com/book/view/1590592123
* Learning the bash Shell
http://www.oreilly.com/catalog/bash3/
* bash Cookbook
http://www.oreilly.com/catalog/9780596526788/
All have sections on optimising your prompt. I don't think my shell prompt is perfect but I've lots the will to play with it any more.
--
"It's Not Magic, It's Work"
Adam
[ Parent | Reply to this comment ]