Colorized OS X Terminal

October 29, 2014comments

I have always wanted the input and output text in the OS X Terminal to be in different colors. Unfortunately no such setting exists in Terminal itself but through some clever Bash tricks one can achieve the next best thing: different colors for prompt, command line and program I/O.

For example:

Colors in OS X Terminal

The idea is to inject ANSI color sequences in the $PS1 prompt string and use a trap to change the color back before commands are executed. I base my implementation on this post. Here are the lines that I have put into my .profile:

PS1="\[\e[0;33m\]\h \w$ \[\e[0m\]"
debug()
{
  echo -n $'\e[0;32m';
}
trap debug DEBUG

Or use this single line installation script:

sudo echo $'PS1=\"\\[\\e[0;33m\\]\\h \\w$ \\[\\e[0m\\]\"\ndebug()\n{\n echo -n $\'\\e[0;32m\';\n}\ntrap debug DEBUG' >>~/.profile

In case it doesn’t work, try inserting the lines into .bash_profile instead.