A shorter prompt

04Dec10

I have moved most of my posts to my new website and will no longer update this blog. From now on, I will continue posting at DayoNtwari.com.

Linux users who work in the terminal a lot might often find themselves with overly long bash prompts, the deeper into their directory tree they get. I have this problem a lot and after searching around a bit, I managed to keep my bash prompt nice and short. Here is what it looks like now:

In the image you can see the prompt now shows only timestamp, username, hostname and current working directory. Home, in this case. And right above the prompt is the output of the pwd command, the current working directory: /home/dayo

I get an automatic pwd output after each cd.

This prompt will only show the current working directory, no matter how deep you go:

My home directory structure is very nested, so this shorter prompt is quite neat. I find this especially handy when delving into system related directories.

To get this shorter prompt, all you need to do is edit your ~/.bashrc file. If you want to make the shorter prompt available system-wide to all users, you will have to put the changes in /etc/bash.bashrc instead.

Append the following to the bottom of your ~/.bashrc or /etc/bash.bashrc:

As you can see, first in the lines before “PS1=..” (which defines the prompt) I defined a bunch of variables to make using ANSI colors easier. In the screenshots, you can see I have customized the colors of my prompt to match the colors of the Rwandan flag. :)

In the “PS1=…” line I have formatted the prompt to show only timestamp (\t), username (\u), hostname (\h) and basename of current working directory (\W). This way, any directory change will only ever show the basename of your current working directory, and not the whole long line of the absolute path. For more prompt options check the man pages for bash and search for the paragraph on “Prompting“.

To have pwd give an automatic output of your current directory after each cd, you need to add the following functions:

Note: Don’t forget, to make your changes to ~/.bashrc or /etc/bash.bashrc effective you will need to save these files and then run exec bash at the command prompt.

That’s basically it. The other functions are things I use to make my work at the command line more efficient and enjoyable. Check out http://www.shell-fu.org/ for more such nifty functions. Edit as required and feel free to share your versions here.