One of the things I learnt during my time at Digg was how to
make better use of the Terminal. And one of the first things I was taught was
how to make changes to your .profile
or .bashrc
files to do so.
I'm going to highlight the way I changed my terminal prompt, both on OSX and Linux.
OSX
Here's the code for OSX, editing the .profile
file:
green=$(tput setaf 2)
yellow=$(tput setaf 3)
reset=$(tput sgr0)
export PS1=" [$green]u@air[$reset] [w] [$yellow]:[$reset] "
export LS_COLORS="di=36:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35"
In order, the above code sets the colour variables, sets the syntax for the
prompt in the PS1
variable, and the ls
command in the LS_COLORS
variable.
This code produces a terminal prompt such as:
onassar@air [~]
I won't go into detail of the prompt syntax/patterns as that's beyond the scope of this (and honestly, I can't remember the flags themselves). But in general, the pattern recognizes two spaces, the username of the account, an @ symbol, the string 'air', a space, followed by a colouring of the current directory (wrapped in braces).
Linux
While the syntax is the same (seeing as OSX is Unix based), here are the differences:
baby=$(tput setaf 6)
yellow=$(tput setaf 3)
reset=$(tput sgr0)
export PS1=" [$baby]u@local[$reset] [w] [$yellow]:[$reset] "
export LS_COLORS="di=36:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35"
See the difference? Not the variable names, but rather the escaping that's
required is the PS1
variable definition.
Also worth noting, is that these rules are defined in the .bashrc
file, rather
than the .profile
file.
Multple Syntaxes
There are some other syntaxes out there which allow you to specify the colour and formatting of your terminal prompt, but the problem I was running into was that the prompt-itself would get overridden when your input was too long.
For example:
export PS1=" e[31;40mu@He[0m [w] e[33;1m: e[0m"
export LS_COLORS="di=36:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35"
This would cause cases like this to occur with long strings of text (or more realistically, long commands): (http://i.imgur.com/lhsQB.png)
While this may seem like an edge-case, it occurs frequently with long commands, or control-c-ing a command you were about to execute.
Colour Breakdown
With help from the post Color Output on Bash Scripts on Linux Tidbits, here's a breakdown of the colours for use:
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
purple=$(tput setaf 5)
baby=$(tput setaf 6)
white=$(tput setaf 7)
And the styling:
bolded=$(tput bold)
underlined=$(tput sgr 0 1)