Skip to content
cnandreu
2 min read

Color Terminal on Mac

Add color to your Mac terminal with a .bashrc, a styled bash prompt, git branch info, and colorized ls output.

I often get asked how to make the Terminal pretty. Beauty is in the eye of the beholder, but hopefully this post will help you along.

.bashrc

First I recommend you create a file called .bashrc in your home directory if it doesn’t exist.

touch ~/.bashrc

In order to get OSX to pick it up you need to add the following to .bash_profile:

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

It’s basically saying: if .bashrc exists in the home directory, load its contents.

Style the Prompt

Open the newly created .bashrc and add the following:

function prompt {

    local WHITE="\[\033[1;37m\]"
    local GREEN="\[\033[0;32m\]"
    local CYAN="\[\033[0;36m\]"
    local GRAY="\[\033[0;37m\]"
    local BLUE="\[\033[0;34m\]"
    local LIGHT_RED="\[\033[1;31m\]"

    export PS1="${GRAY}\u${GREEN}@${CYAN}\W${GRAY} \$(__git_ps1 '(%s)')${GRAY}$ "
}
prompt

Inside the prompt function a few colors have been defined (e.g. WHITE, GREEN). You can use these colors to style the typical user@host ~ message that shows up before any command is typed. For example:

export PS1="${GRAY}\u${GREEN}${GRAY}"

Will output your username in a gray font (${GRAY}\u), a green arrow (${GREEN}→), and everything after the green arrow will be gray (${GRAY}).

The bash prompt variable reference lists them all (i.e. \u for username).

Git

The __git_ps1 '(%s)' command can be used to display the current git branch you’re on. For example, typing that into the Terminal while inside a folder that contains a .git folder will display something like (master) or (branch_name).

You need to include these lines at the top of your .bashrc to get the git prompt and git auto-completion:

source /usr/local/etc/bash_completion.d/git-completion.bash
source /usr/local/etc/bash_completion.d/git-prompt.sh

Those files may be in another folder on your machine — I installed git with Homebrew, a package manager for OSX.

If you don’t have git but want it installed, try installing it via Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
brew install git

I took the first line from Homebrew’s home page.

It’s a good idea to version control your configuration file. Try adding your .bashrc to a gist.

More Color

Paste the following into your .bashrc for even more color:

export LS_OPTIONS='--color=auto'
export CLICOLOR='Yes'
export LSCOLORS='Bxgxfxfxcxdxdxhbadbxbx'

alias ls="ls -G"

The -G flag will enable colorized output. Type man ls for more details.