Snippet: A useful, minimal, colored Bash prompt

Posted by Kiyoshi Murata Fri, 25 Jun 2010 04:23:00 GMT

Previous note: this post isn’t of very much use for sysadmins, I’m a software developer

I always wanted to have a fresh new terminal prompt but never got to stop everything to actually research about it, because I didn’t really knew much about it besides the basic export PS1="> ".

Finally I did this and was kind of frustrated by not finding many sources. I found many that were useful, like the Bash how-to at TLDP, but, you know, I was initially after a “bash prompt gallery” to take a look at and grab one of my choice. There aren’t none of that, except this good blog post from MakeTechEasier, from which I didn’t really find what I was looking for.

I wanted a simple, minimal, coloured and also “beautiful” prompt, but besides all, it had to be useful. To accomplish this, I went then to gather the information I should display on the screen.

The first urge of many people (and I infer that from the many examples I found) is to put many cool things, like CPU load, bash command history number, last command return value, time, current directory, username, hostname, a “floating” clock on the right up corner of the terminal… ah, come on. You have to remember that these things will show up every time you hit enter. If you want to play pacman on your prompt, that’s fine, but I realised that you have to focus on what you’re doing, nothing else. Again, when you are woke for, say, 20-24 hours, or working in the middle of the night, you don’t want things blinking, buzzing or stealing your attention. You don’t want a Crazy POWERFUL bash prompt when you actually want to write commands and hopefully go to sleep.

So I went for the radical minimalistic, almost hidden, prompt. To get there, I started a list of the most essential things I will not live without, to then remove the useless ones. I started like:

  1. Current directory

… then I suddenly stopped and tried to think of anything else. And simply there aren’t other essential things. By reading it in cold like that, you may disagree, but if you take the time to do the mental exercise of actually thinking of it, even things that at first seem to be critical like the time and date, aren’t really necessary to be shown at every single command (if you are after seeing how long a command takes, there are tools out there to do it). Take username or hostname too, how often do you think those things change when you’re at your personal notebook?

So that’s that, now I have to show only one information in a nice way.

After trying some colors and layouts, I was uncertain about a two-line prompt. At first, it seemed to be a lot like one of these bloated prompts, but after some testing, I finally decided to go for it, mainly because I often encounter large directory names like in my music collection.

From, here, the prompt was basically set. I looked at it and saw it could have it show more information, but in a implicit way: changing the color of the arrow on the prompt to reflect the last command status, it is, if it was successful, it should be green, if failure, red.

Now this is completely personal

Since I’m a software developer that uses git for everything, I decided to put the current checked out branch after the current directory. But if CWD is not a git repository, show nothing, as expected.

The final result was this:

A minimalistic but useful bash prompt

The code you can grab here: http://gist.github.com/450459

To use it just add those lines to your ~/.bash_profile, taking care of replacing possible old ones.

Snippet: A sed shell in Python

Posted by Kiyoshi Murata Fri, 21 May 2010 07:17:00 GMT

Here is a very simple shell in Python to test regular expressions with sed.

The shell works by matching the input as a sed command against a given data.
Each input line is evaluated as a Python expression (and it must evaluate to a [base]string) and then sent to sed as a command to be matched against this given data.
So every input is the same as:

echo <data> | sed -r -e <input>

<data> is set by using the d() function:

>> d("My data")
>> r's#^My#Our#'
Our data

To see what <data> contains, simply use the d() function without arguments:

>> d()
My data

(<data> defaults to the alphabet string "abc...z")

To exit, use Ctrl-C (KeyboardInterrupt) or send an EOF to it (Windows: Ctrl-Z+Enter, Linux: Ctrl-D) or yet use the exit() function.