Compact-pwd patch for bash

download home

INTRODUCTION

It is very convenient to display the working directory in the shell prompt. This, however, can get very annoying as well, when you cd to a directory with long name. The prompt gets inconveniently long in this case.

I have written a remedy for this problem. After rebuilding bash with my patch, if you replace \w with \S in the PS1 parameter, the working directory will be displayed abbreviated. Only the first letter of each directory component is displayed, and maybe some other letters to distinguish the directory from other files or directories. Let's see some examples. Propose I set PS1='\S\$ '. If I cd to the directory

/usr/lib/perl5/site_perl/5.005/i586-linux/auto/QRadioButton

the prompt is:

/u/li/pe/s/5/i/aut/QRad$

This means that if filename completion is enabled, you can cd to this directory by typing:

cd /u<TAB>/l<TAB>i<TAB>/p<TAB>e<TAB>/s<TAB>/5<TAB>/i<TAB>/a<TAB>u<TAB>t<TAB>/Q<TAB>R<TAB>a<TAB>d<TAB><ENTER>

You can still display the full name of the directory with the pwd command.

There are some special exceptions: the /usr/local and the /usr/share directories are printed as /u/l and /u/s respectively, instead of /u/lo and /u/sh. Also a tilde (~) is used instead of $HOME, as bash does it already. For example

/usr/local/Wolfram/Mathematica/5.0/AddOns/JLink/Documentation/JavaDoc/com

is displayed as:

/u/l/W/M/5/A/J/D/J/cm$

And /usr/share/man/man1 is displayed as /u/s/ma/m1.

If you do not have read access for the parent of a directory, the directory will be spelled out fully.

DETAILS

The patch is written for bash version 2.05b and version 3.0. It may or may not work for other versions. It may also be buggy.

I only tested the patch on i686 Linux, it may not be portable for other systems. This is true especially for the readdir call.

Apply the patch with the -p option. Do not forget to rerun configure if you apply the patch to an already configured bash, as the patch adds new #include dependencies. Here is an example to build bash with the patch.

$ tar -xzvf bash-3.0.tar.gz
$ <compact-pwd.patch patch -p0
$ cd bash-3.0
$ ./configure
$ make
$ make install

Update: Stephane Chazelas has noted that the same function can be implemented in pure bash, by using promptvars.

DOWNLOAD

You can download the patch here. Make sure to load the version matching the version of your bash.

bash-3.0b-compact-pwd.patch

bash-2.05b-compact-pwd.patch

This patch does not come with any warranty at all. I am not sure if it works in all cases or not. Use at your own risk.

AUTHOR

Zsban Ambrus <ambrus@math.bme.hu>