Mac OS X Terminal Customization: Colors

23 December 2015 on software and opensource.

The out-of-the-box configuration of the Terminal application in Mac OS X is quite simple/plain. If you don’t use it frequently, it will be ok to leave it in that way. However, if you continuously open Terminal, it will be great to customize it so you can be more productive.

By default, this is how Terminal application looks like (Basic Profile):

01

As you can see, it is based just in black text and white background but you can give more life to Terminal using colors and this article will explain how to customize it according to your own tastes.

Mac OS X Terminal

First, let’s change the default profile to other one . To do so, open a new Terminal and go to Terminal -> Preferences -> Profiles. In my case, I like the Pro Profile (cool name!) because it looks like an old terminal with white text and black background.

01

Besides changing the profile, you should consider enabling the following options in Text Tab:

  • Antialiases text: To apply text smoothing. It is specially useful with some kind of monitors but you can leave it enabled any way.
  • Use bold fonts: To use bold for emphasis. Must be enabled.
  • Allow blinking text: To allow text to flash on and off. Highly recommended.
  • Display ANSI colors: To display text using the colors embedded in some terminal emulator standards. Must be enabled.
  • Use bright colors for bold text: To add emphasis to bold text with color. Must be enabled.

Configuring BASH

Now it is time to configure the BASH prompt; that means, change the environment variable $PS1. Let’s check its default value:

optimus:~ modlost$ echo $PS1
\h:\W \u\$

The prompt shows the hostname (\h), colon (:), current directory base name (\W), username (\u) and # or $ symbol ($). You can find more information about all the backslash-escape especial characters available for this variable in the unix man page for BASH section PROMPTING. In this case, we are going to use these options:

  • \u: The username of the current user.
  • \h: The hostname up to the first ‘.’.
  • \w: The current working directory, with $HOME abbreviated with a tilde.
  • $: If the effective UID is 0, a #, otherwise a $

In my experience a simple (short) prompt works best and Debian distribution defines one by default that I really like. Take a look at it executing the following command:

optimus:~ modlost$ PS1='\u@\h:\w\$ '
modlost@optimus:~$  

Looks great but it will be better with colors. To help you decide which color you would like, you can use this shell script to print the 16 ANSI color codes. Execute it in a Terminal using your prefered profile so you can decide which colors are best for you.

01

Now you can choose where you want to change colors coping those codes in $PS1. Debian distribution define a nice colors, let’s use the same:

modlost@optimus:~$ PS1=‘\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[m\]\$ ‘

01

To make these changes permanent you should define $PS1 in .bash_profile because Terminal starts login shells. At the end of the next section you will find a complete ~/.bash_profile.

Configuring ls and grep commands

If you use the Terminal a lot, it is probably that ls is one of your most used commands so adding color will be awesome. To do that, you will have to define a new enviroment variable LSCOLORS containing a string that has the following structure:

  1. directory
  2. symbolic link
  3. socket
  4. pipe
  5. executable
  6. block special
  7. character special
  8. executable with setuid bit set
  9. executable with setgid bit set
  10. directory writable to others, with sticky bit
  11. directory writable to others, without sticky bit

Each one of those items contains two characters, one for the foreground and the other for the background. The color codes you can use are:

  • a black
  • b red
  • c green
  • d brown
  • e blue
  • f magenta
  • g cyan
  • h light grey
  • A bold black, usually shows up as dark grey
  • B bold red
  • C bold green
  • D bold brown, usually shows up as yellow
  • E bold blue
  • F bold magenta
  • G bold cyan
  • H bold light grey; looks like bright white
  • x default foreground or background

For Pro Profile that uses dark background, this could be one good color combination:

modlost@optimus:~$ LSCOLORS=ExFxBxDxCxegedabagacad

But it doesn’t work unless you add the -G option in every ls execution, so the best way to do that is defining aliases:

modlost@optimus:~$ alias ls='ls -G'
modlost@optimus:~$ alias ll='ls -l'
modlost@optimus:~$ alias la='ls -A'
modlost@optimus:~$ alias l='ls -CF'

Execute them to test the configuration:

01

Another quite common command is grep and it also supports colors, you just need to define the following alises:

modlost@optimus:~$ alias grep='grep --colour=auto'
modlost@optimus:~$ alias fgrep='fgrep --colour=auto'
modlost@optimus:~$ alias egrep='egrep --colour=auto'

Execute a little example and you will see how useful is enabling colors in grep:

01

So the final ~/.bash_profile is the following:

# PROMPT CONFIG
export PS1="\[\e]0;\a\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[m\]\$ "

# COLOR COMMANDS
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -G'
alias grep='grep --colour=auto'
alias fgrep='fgrep --colour=auto'
alias egrep='egrep --colour=auto'

# ALIASES
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'

Configuring Vim

Finally, if you use Vi Editor a lot, you should create a ~/.vimrc file with the following content:

syntax on
set background=dark

And the result will be the following:

01

Next Steps

Now your Mac OS X Terminal Application has a lot of colors and that will help you every day and make you more productive.

Happy Hacking!!!!


Previous <<< @ >>> Next