«

Project: pwdshort – Bash Shell CWD Shortener

As a hardcore linux user, I frequently use the bash shell. As a developer I often have directories with long names, often with many subdirectories.

The problem I was encountering was that the CWD displayed in my $PS1 prompt was so long that I couldn’t fit longer commands without reaching the end of the line. With this in mind, I had two options: Modify $PS1 so that it looked like this:

/home/user/development/projects/websites/permafrostcodingstudio.com
$

Personally, I don’t like this. I prefer to have everything on one line. So I came up with an alternative based on an old script (which I would link to here but do not recall where I found it):
Still display the CWD in the prompt, but abbreviate it.
Thus, pwdshort was born.

pwdshort takes the output of the environment variable $PWD and removes most of the directory names, replacing them with an ellipsis. It keeps the first and last directory names (which is configurable), resulting in a much shorter CWD output.

The result:
Before:

user@machine:/home/user/development/projects/websites/permafrostcodingstudio.com/
code.permaforstcodingstudio.com/templates/include $

After:

user@machine:~/permafrostcodingstudio.com/.../templates/include $

As you can see, it still lets you know where you are, but eliminates unnecessary data. In addition, another useful feature is the ability to replace the output directory names with something else, i.e., instead of “/templates/”, it could output “/tpl/”. This is all done in the configuration file as well, pwdshort.json.
Using directory name replacement, we could shorten the PWD even more, resulting in something like:

user@machine:~/permafrostcodingstudio.com/.../tpl/inc $


As of this article, the current version is v1.1.1.

To use this project in your bash shell, see the instructions README.md file in the GitHub project.
View the pwdshort project on GitHub.


Permanent link to this article: http://permafrostcodingstudio.com/articles/project-pwdshort-bash-shell-cwd-shortener/

Leave a Reply

Your email address will not be published. Required fields are marked *