20130517

Pimp my .bashrc


To your average pleb, the ~/.bashrc file is pretty worthless. However to sysadmins and *nix geeks alike, it is a thing of great importance, and there are many who spend hours upon hours getting it just right. I am one of those people. For me, the bash prompt itself is very important as it is what I am going to see every time I hit enter in my terminal. I'm going to try and keep this brief (although I doubt I will), so without further ado, here is my current bash prompt:



As you can see, I am using a multi-line prompt. The first line, in order, shows my user@host, my current working directory and my bash history number. The second line shows my command number and my effective UID. The general ascii prettiness was achieved through putting the terminal into line-drawing mode.

function prompt {
Black="\e[30m" 
Red="\e[31m"

Green="\e[32m"
LightGreen="\e[32;1m" 
Blue="\e[34m"
 
LightBlue="\e[34;1m"
 
NC="\e[0m" # No Color

PS1="\[$LightGreen\033(0\154\033(B\][\[\033(0\161\161\033(B$Green\][\[$Red\]\u@\h\[$Green\]]\[\033(0\161\033(B\][\[$Red\]\w\[$Green\]]\[\033(0\161\033(B\][\[$Red\]\!\[$Green\]]\[\033(0\161\161\033(B\]]\n\[$LightGreen\033(0\155\161\033(B$Green\][\[$Red\]\#\[$Green\]]\[\033(0\161\033(B\][\[$Red\]\$\[$Green\]]\[\033(0\161\161\033(B\][\[$NC\]"
PS2="> " 
}
 
prompt
It may seem pretty complicated, but once you get the hang of it, it really isn't too hard to figure out:

\033(0 puts the terminal in line-drawing mode, and then \033(B takes it out of it. This means that by putting certain numbers between those sequences, you can make some rather pretty shapes. For a full list of characters and their numerical representations, check out this guide.

Here is a full list of the escaped characters and what they do:

If you look at the line of code defining the variable PS1, you will see a few of these escaped characters in there. You will also see a number of different colour variables. These variables are defined at the top of the file, because typing out \e[30m just to change the following text to black is pretty annoying. Thus I created a variable for each colour enabling me to just type $Black instead.

Sifting through the mess of characters defining the PS1 variable, you will also find a lot of \[ and \] escapes. These are to define non-printing characters so that you don't completely fuck over your word wrapping. Pretty much anything that does not move the cursor forward is a non-printing character (colour codes, escaped keys, etc.). I found adding those to be invaluable as my terminal window isn't always the same size, so if I happen to use a terminal in one of the smaller frames in RatPoison, it would render horribly if I didn't use those escape sequences to delimit my non-printing characters.

This took me quite a while to get just right, and I think it's a pretty fun gimmick and something to mess around with when you just absolutely have nothing. else. to do.

No comments:

Post a Comment