Add Color to the Terminal in Mac OS X

Feb 21, 2012 - 22 Comments

Color Terminal

Adding colorized ls output to the Terminal in Mac OS X is a good way to make navigating around the command line a bit easier on the eyes. This makes different items show up in different colors, including directories, files, executables, and symbolic links.

How to Add Color ‘ls’ Command Output in Mac OS X Terminal

We’ll cover a customizable color output setting for both dark and light terminals, and you can get a preview of color ls output by typing “ls -G” at the command line. The preview with ls -G will depend on the Terminals color settings and won’t necessarily represent the colors shown in the screenshots below, however.

  • Open Terminal and type:
  • nano .bash_profile
  • Use the arrow keys to navigate to the bottom of the document and paste in either of the blocks of text below, depending on the terminals appearance (see man entry below for further customizations)

Colors for Dark Terminal Themes:
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

Dark Terminal Colors

Colors for Light Terminal Themes:
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad

Light Terminal color theme

  • After the strings are pasted into .bash_profile confirm that it looks something like this in nano:
  • Color Terminal string in bash profile

  • Hit Control+O to save and open a new Terminal window
  • Type “ls” or “ls -la” to confirm the colorized output

Optionally, you may want to create an alias in .bash_profile to link ls to something like ls -GFh, this would look like:

alias ls='ls -GFh'

This will work with Mac OS X 10.6, OS X 10.7, OS X 10.8, and beyond, as long as you are using the bash shell. If you aren’t certain what shell you are using, look in the Terminal window titlebar for “bash”, or you can check with the following command:

echo $SHELL

The output will be “/bin/bash” if it’s bash, and something else if not.

Don’t forget that you can also change the appearance of Terminal windows instantly and change the Terminal wallpaper too.


Customizing LSCOLORS Manually
If the above color choices weren’t doing it for you, you can set whatever you want. Here’s the manual page on LSCOLORS if you want to give it a shot. The default is “exfxcxdxbxegedabagacad” but clearing out the .bash_profile color entry will remove any hideous color combinations too.

LSCOLORS The value of this variable describes what color to use
for which attribute when colors are enabled with
CLICOLOR. This string is a concatenation of pairs of the
format fb, where f is the foreground color and b is the
background color.

The color designators are as follows:

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

Note that the above are standard ANSI colors. The actual
display may differ depending on the color capabilities of
the terminal in use.

The order of the attributes are as follows:

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

The default is “exfxcxdxbxegedabagacad”, i.e. blue fore-
ground and default background for regular directories,
black foreground and red background for setuid executa-
bles, etc.

.

Related articles:

Posted by: William Pearson in Command Line, Customize, Mac OS, Tips & Tricks

22 Comments

» Comments RSS Feed

  1. Joe says:

    Marvellous! Many thanks for that. Now my Mac’s as colourful as Linux.
    This may be just me, but the -h option for the ls command depends on a long output (is that a change that’s come recently? I’m using High Sierra). So…
    ls -GF
    …will yield the same results as…
    ls -GFh
    …so you’ll actually need…
    ls -lGFh #that’s a lower case letter “ell”

    • FeRDNYC says:

      The point of aliasing ‘ls’ to “ls -GFh” is that you can type ‘ls’ to get short output, but ‘ls -l’ will give you long output _with_ human-readable sizes.

      IOW, the expanded command is the alias _plus_ any arguments you pass it. With the suggested alias:

      ls => ls -GFh /* (Yes, the -h is ignored here.) */
      ls -l => ls -GFh -l

      If you alias ‘ls’ to “ls -lGFh” you can never get a short listing.

  2. tahootyhoot says:

    Thanks! This worked beautifully!!!

  3. Brian Wright says:

    While this is a great tutorial, its color scheme is completely different to Linux. When moving back and forth between Mac and Linux, the two differing color schemes always bothered me (directories purple over here, cyan over there, etc). In fact, I’ve yet to find a single LSCOLORS scheme that mimics Linux. So, here it is:

    CLICOLOR=1
    LSCOLORS=ExGxFxdxCxDxDxHBhDhCgC
    export CLICOLOR LSCOLORS

    This scheme is as close as I find to a match with Linux’s ls –colors. Though, Linux’s ls –colors actually offers more features than Mac’s somewhat simplistic equivalent.

  4. Kurt__ says:

    Nice One ! Ive only had a MAC for about 11 years and now I have colours ! :)

  5. user123 says:

    It worked very well.
    My terminal looks so much better now.

    Thank you very much! =D

  6. user says:

    It worked very well. My terminal looks so much better now. Thank you.

  7. Ramesh says:

    My screen looked so boring and was difficult on eye difficult to differentiate file from directory. Now its all easy to see.Thank you very much.

  8. Mike says:

    I forgot to say that you can colourise grep (and fgrep and egrep) without resorting to aliases. The following will suffice in a .bash_profile or such like:

    export GREP_OPTIONS=’–color=auto’
    export GREP_COLOR=’1;35;40′

  9. Mike says:

    Thanks for your tip. Great for ls.

    I copied colordiff to /usr/bin and put ‘alias diff=colordiff’ in my .bash_profile. (colordiff is a pyhton script that performs a colourised diff).

    I also use:

    alias grep=’grep –color=auto’
    alias fgrep=’fgrep –color=auto’
    alias egrep=’egrep –color=auto’

    Which seem to work.

  10. […] run ls, and see the difference. Still not satisfied with the appearance, or have you already done that? There’s more to […]

  11. Jefferson Borges says:

    Hi!
    Really nice post.. thanks a lot!
    I’ve been looking for this for like thousands..

    A question: Is there a way to make these parameters be valid when you open an SSH session?
    I ask it because when I open an SSH connection, it gets back to the default colorless version.

    Thanks!

  12. John says:

    I try to save .bash_profile using Control+O command, but it’s doesn’t work..

    • MDS says:

      put sudo in front of it…

      sudo nano ~/.bash_profile

      it will ask you for your password then you can edit away on the file.

  13. […] recently covered the classic method of adding colors to the command line by editing .bash_profile, but it turns out that Terminal in OS X Lion and OS X […]

  14. Ron says:

    When I use Control+O to sad it asks what name to save it as. Can I use any name, since it will not let me save it as the existing name?

  15. James says:

    Add the IR_Black theme to Terminal here – http://blog.toddwerth.com/entries/13

    IR_Black comes from a Textmate theme and uses more pastel colors that just are more visible on a dark black background.

    Then just make it the default settings. The only change I would make is a choice of a better fixed width font and change the highlight color to something brighter.

  16. vdiv says:

    Excellent tip! What if one uses xterm under X11?

    • vdiv says:

      Ah yeah, for non-login shells the settings should be saved in a .bashrc file in the home folder (i.e. xterm under X11)

  17. Mrs.Hopper says:

    Wow! Love this tip. Thank you very much

  18. James says:

    Terminal looks so boring in this day and age, whatever happened to TermKit? It hasn’t been updated since it first was released, so much hope and it went no where.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site