Do you use let's encrypt?





7703 votes ~ 25 comments

 

Controlling the size of the $PWD in bash

Posted by mikhailian on Wed 12 Sep 2007 at 10:22

Tags: ,

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.

 

 


Re: Controlling the size of the $PWD in bash
Posted by Anonymous (137.222.xx.xx) on Wed 12 Sep 2007 at 12:07
Alternatively, you can just use \W in the prompt which is the current working directory truncated to the last dir.

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 ]

Re: Controlling the size of the $PWD in bash
Posted by sdusart (64.236.xx.xx) on Wed 12 Sep 2007 at 12:40
Quite useful indeed, thanks !

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 ]

Re: Controlling the size of the $PWD in bash
Posted by Anonymous (89.140.xx.xx) on Wed 12 Sep 2007 at 14:25
My PS1 var:
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 ]

Re: Controlling the size of the $PWD in bash
Posted by dkg (216.254.xx.xx) on Wed 12 Sep 2007 at 22:58
[ View dkg's Scratchpad | View Weblogs ]
It's also useful to include $? in your prompt so you can see what the return code of the last command was. i usually place it, followed by a space, just before the username in my prompt.

[ Parent | Reply to this comment ]

Re: Controlling the size of the $PWD in bash
Posted by mikhailian (84.199.xx.xx) on Thu 13 Sep 2007 at 14:51
Er... after reading the insightful comments above, I would like to offer a much shorter version. Tested on GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu).
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 ]

Re: Controlling the size of the $PWD in bash
Posted by mcortese (213.70.xx.xx) on Mon 17 Sep 2007 at 09:22
[ View Weblogs ]
Quite similar to what I have in my .bashrc. I only adapt the length of the path to the width of the terminal: it must be one third (including the initial "...").
function truncate_pwd
{
  newPWD="${PWD/#$HOME/~}"
  local pwdmaxlen=$((${COLUMNS:-80}/3))
  [ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...{newPWD:3-$pwdmaxlen}"
}

[ Parent | Reply to this comment ]

Re: Controlling the size of the $PWD in bash
Posted by Anonymous (84.56.xx.xx) on Sun 30 Sep 2007 at 01:02
Here is the above with the missing $ compiled into a skript to extend your current setup smothly (replaces \w with the shortened path):
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 ]

Re: Controlling the size of the $PWD in bash
Posted by Anonymous (83.214.xx.xx) on Sun 16 Sep 2007 at 23:45
with zsh :
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 ]

Re: Controlling the size of the $PWD in bash
Posted by Anonymous (122.162.xx.xx) on Fri 21 Sep 2007 at 08:09
"it seems debian-administration's competence is cratering :("

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 ]

Re: Controlling the size of the $PWD in bash
Posted by Anonymous (83.214.xx.xx) on Fri 21 Sep 2007 at 19:50
It was hopefully exaggerated, I was just surprised (and a bit angry) of reading a small bash tip here (and I'm not "pretty good").
(btw excuse me if the author felt offended)

[ Parent | Reply to this comment ]

Re: Controlling the size of the $PWD in bash
Posted by ajt (204.193.xx.xx) on Tue 25 Sep 2007 at 10:11
[ View Weblogs ]
This is an interesting topic, that is covered in many books. It's also very personal, do you like long or short, one line or two, to see the error code or not, to see the job number or not, in colour or not...

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 ]