Bash PWD Shortening

Viewed 12610

I'm looking for a bash function that will shorten long path names to keep my PS1 variable from getting excessively long. Something along the lines of:

/this/is/the/path/to/a/really/long/directory/i/would/like/shortened

might end up as:

/t../i../t../p../to/a/r../l../d../i/w../like/shortened

something that the took the path and a maximum acceptable number of characters to shorten to would be perfect for my .bashrc file.

8 Answers

Try this:

PS1='$(pp="$PWD/" q=${pp/#"$HOME/"/} p=${q%?};((${#p}>19))&&echo "${p::9}…${p:(-9)}"||echo "$p") \$'

It transforms

~/.vim/bundle/ack.vim/plugin

to

.vim/bund…im/plugin

transfrom

/usr/share/doc/xorg-x11-font-utils-7.5/

to

/usr/shar…utils-7.5

And when $PWD same as $HOME, show nothing.

Bonus: you could modify number of length to fit you need.

Related