OS X Lion Terminal Colours

OS X is awesome for command line stuff, that’s why real programmers love it. But some of the defaults Apple have used make me sad. Where are all the colours? I demand my ls command be pretty and colourful! Why is Vim so bland? While we’re at it, can’t ping lick my eyeballs more?

Good news: yes, they can! I’ve stuck some tips together so I can remind myself what to do when I inevitably reinstall my system for some pointless reason, and for any new Mac users out there. It is near peak commercial season after all, and I’m sure you’ve been good little boys and girls.

Let’s start with the simple things.

Making ls and grep Colourful

Add these lines to your .bash_profile file which is in your home directory:

~/.bash_profile
1
2
3
4
5
# Tell ls to be colourful
export CLICOLOR=1

# Tell grep to highlight matches
export GREP_OPTIONS='--color=auto'

Now when you use ls, folders and executables will appear in different colours from normal files, and when you use grep to find text in files, the matches will be highlighted.

You can change the colours used by ls via the LSCOLORS environment variable. For example:

~/.bash_profile
1
2
3
4
5
6
# Tell ls to be colourful
export CLICOLOR=1
export LSCOLORS=Exfxcxdxbxegedabagacad

# Tell grep to highlight matches
export GREP_OPTIONS='--color=auto'

Here’s a handy website you can use to define your LSCOLORS scheme: http://geoff.greer.fm/lscolors/. It also generates LS_COLORS definitions, useful if you’re a Linux user. One thing to note, though, is that the colours you will see in your terminal depend on the particular theme you are using. Which brings me to my next tip.

Using A Better Terminal Theme

If you haven’t changed the default terminal theme you must still be gazing in to the snowy wasteland that is the default. This is insane. Programmers are incompatible with white backgrounds. Er, except in blogs. *cough*

Changing the terminal theme is marginally trickier than it should be. Here’s how you do it:

  1. Go to TerminalPreferences…
  2. Select the Settings group.
  3. Pick something nice that doesn’t sear your retinas.
  4. At the bottom of the list box, click Default so that new terminals will open with the selected colour scheme.

Anything other than the default is a good start, but there are better themes out there than those bundled with Lion. At the moment, my colour scheme of choice is Solarized. You can get a Solarized scheme for OS X’s Terminal from here:

If you are running Lion, ignore the README at the site. All you have to do to install the theme is to open the *.terminal file. That should be enough to make it appear in the themes list in the preferences dialog, where you can set it as default.

Solarized is a great general purpose theme and there are settings for many text environments - I use it in Xcode, Sublime Text 2, Eclipse and even Vim.

Which, niftily, brings me on to my next section.

Making Vim Pretty

Much as I’ve rallied against Vim over the years, Vizio360 and Rev. Kachowski have now drugged me sufficiently to admit that it’s pretty damn nice really. If you can handle memorising π to 300 digits, then you should be fine with Vim shortcuts. On the other hand if you’re like me, you may want to use a cheatsheet.

However, we aren’t here to learn, we’re here to colourise. So, in order to make Vim a bit more colourful on OS X, copy the example vimrc file to your home directory:

cp /usr/share/vim/vim73/vimrc_example.vim ~/.vimrc

Now you can enjoy pretty colours in Vim. If you want to add Solarized as your colour theme in Vim, I recommend installing the rather fantastic pathogen.vim in your .vimrc (instructions) and then follow the instructions for “Option 2” in vim-colors-solarized README.

Making Nano Pretty

If Nano is your Terminal text editor weapon of choice, then you can enable syntax colouring by uncommenting the syntax files referenced in /etc/nanorc. First, open it for editing:

sudo nano /etc/nanorc

Move to the bottom of the file and then remove the # character at the beginning of the lines which include other *.nanorc files. For example, change:

/etc/nanorc
1
2
## Nanorc files
#include "/usr/local/share/nano/nanorc.nanorc"

To:

/etc/nanorc
1
2
## Nanorc files
include "/usr/local/share/nano/nanorc.nanorc"

Further Eyeball Licking via Homebrew

For the next tip, I recommend using Homebrew as the method of installation. If you aren’t using Homebrew yet, you probably should be. It’s by far the Most Awesome package manager available for OS X. So go install it:

Installed? Good. Let us continue.

Generic Colouriser

Generic Colouriser is a great utility which can be used for colourising many different types of output and log files. If you installed Homebrew , installing grc is as simple as typing:

brew install grc

NOTE: If you followed the first tip and created a .bash_profile, rather than appending the command to load grc to your .bashrc, you want to append it to your .bash_profile file instead:

echo 'source "`brew --prefix grc`/etc/grc.bashrc"' >> ~/.bash_profile

If you are confused as to why, it’s because the logic Bash uses for loading these files is non-obvious. For a good description of the load sequence, read Execution sequence for .bash_profile, .bashrc, .bash_login, .profile and .bash_logout by Ramesh Natarajan.

After you have added that line to your .bash_profile, either start a new session, or reload the profile:

source ~/.bash_profile

Now when you use certain commands such as traceroute, the output should be colourised:

Or you can call tail and head using grc to colourise log output:

That’s all Folks!

Well, that’s all I have for now. If anyone has any other good ones, stick them in a comment below.

Comments