8

For example working in:

/home/stuart/src/project/com/example/package
/usr/lib/python2.6

would appear as:

~/s/p/c/e/package$ 
/u/l/python2.6$

3 Answers 3

3

Yes, it is possible and has been done here.

8

Try this:

export PS1='$(echo $(dirname \w)|sed -e "s;\(/.\)[^/]*;\1;g")/$(basename \w) $ '
3
  • Great thanks. I've updated it to: export PS1='$(echo $(dirname \w)|sed -e "s;\(/\.\?.\)[^/]*;\1;g" -e "s;/h/s;~;")/\W\$ ' So that dot dirs keep a "." and a character. I also replace "/h/s" (/home/stuart) with a "~".
    – Stuart K
    Apr 17, 2011 at 9:19
  • 2
    Second update: export PS1='$(echo $(dirname $(echo \w | sed "s;$HOME;~;"))/ |sed -e "s;\(/\.\?.\)[^/]*;\1;g" -e "s;/h/s;~;" -e "s;\./;;")\W\$ ' Adds some more hacks so that it looks good in the home dir.
    – Stuart K
    Apr 17, 2011 at 9:33
  • And now I've discovered there are far too many edge cases for this approach to work the way I want (root dir, in home dir, being below home dir). It's still very elegant though!
    – Stuart K
    Apr 17, 2011 at 9:42
1

Here is my go at it, based on Mat's answer. Avoids problems in corner cases such as / and ~.

PS1='`echo "${PWD%/*}" | sed -e "s;\(/.\)[^/]*;\1;g"`/${PWD##*/} \$ '

Replace . with .. to display 2 letters.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .