My First Approved Script on CodeCanyon.net
Mar, 2010
30

LSCOLORS in OSX Snow Leopard, For Dummies

So after cruising the web for a few hours trying to find ‘dummy’ style documentation on changing colors in terminal using .bashrc, I finally stumbled across a site that got me half way there. After playing around a bit I found a few little tricks and things that no one really likes to share. So here it is.

LSCOLORS A bash Command

The first thing you should know is that this is not as straight forward as most people like to make you believe. I am a total n00b when it comes to bash so I really need to start at the beginning.

Step 1: Make sure .bashrc is being used by terminal

This is the easiest step. In terminal navigate to your home directory. This is usually accomplished by typing ‘cd ~‘ and executing. Then do a quick directory listing showing all hidden files ‘ls -al‘. Once you do this, you should see both a ‘.bashrc‘ and a ‘.bash_profile‘. If you don’t, no need to panic we can create them easily and Snow Leopard is smart enough to know what to do with them.  So if they don’t exist, execute these 2 commands in terminal ‘touch .bashrc‘ ‘touch .bash_profile‘. If you re-run your ‘ls -al‘ again you should now see the files.

Step 2: Link .bashrc to .bash_profile

Using your favorite editor, in my case Textmate, open ‘.bash_profile’. This file needs one line.

source ~/.bashrc

Update: A good friend of mine at blog.ubrio.us just gave me the scoop on ‘bashrc‘ vs ‘bash_profile‘. The gist of it is that bashrc is read when you open a new tab in terminal locally and bash_profile is read when you are SSHing into the environment. So choose whichever works for you. I just link them because I want the same view no matter how I am connecting.

Next open the ‘.bashrc‘ file, this is where we can have some fun.

Step 3: Colors LSColors and more LS_Colors

The first thing we have to do is prepare bash to receive the colors, in order to do that you need to enter these 3 lines.

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

This should be pretty straight forward, we are exporting the LS_OPTIONS and saying to use colors and to automatically choose them for us if I have not chosen them. CLICOLOR=’YES’ is telling bash to force color output and LS_COLORS is where we will start to define our scheme.

Here are the colors that are available:

  • a = black
  • b = red
  • c = green
  • d = brown
  • e = blue
  • f = magenta
  • g = cyan
  • h = light gray
  • x = default

It is important to note how the color codes pair up. Basically each entry will be a Foreground | Background pair. so ‘ex‘ means foreground blue background default. The ‘default’ color will be whatever you have set in your command line app’s preferences. Most default to a white background and black foreground. By simply upper-casing any of the color codes you can bold your text, this really on works on foregrounds. Example: Bx means bold red foreground and default background.

The next thing that I found difficult to find was how these color codes were ordered. So it’s like this….

  1. DIR
  2. SYM_LINK
  3. SOCKET
  4. PIPE
  5. EXE
  6. BLOCK_SP
  7. CHAR_SP
  8. EXE_SUID
  9. EXE_GUID
  10. DIR_STICKY
  11. DIR_WO_STICKY

Wow that’s a lot, but that’s the order..

Step 4: Putting it all together

So now you have the order and you have the colors, lets put it all together. I use a default background of black and a foreground of white, so I use a lot of default color settings.

export LSCOLORS='Bxgxfxfxcxdxdxhbadbxbx'

Here is the break down:

  1. DIR = Bx #Bold red text with default background
  2. SYM_LINK = gx #cyan text with default background
  3. SOCKET = fx #magenta text with default background
  4. PIPE = fx #magenta text with default background
  5. EXE = cx #green text with default background
  6. BLOCK_SP = dx #brown text with default background
  7. CHAR_SP = dx #brown text with default background
  8. EXE_SUID = hb #light grey text with red background
  9. EXE_GUID = ad #black text with brown background
  10. DIR_STICKY = bx #red text with default background
  11. DIR_WO_STICKY = bx #red text with default background

Step 5: Wrap UP

So your done, save your bash files and restart terminal. You should now have colors.

Well that’s it, I hope this helps you figure out your bash color profile: :) cheers!

Post to Twitter Tweet This!

This entry was posted on 03.30.2010 at 7:23 pm and is filed under Code Bin, Unix Pie. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Comments

3 Responses to “LSCOLORS in OSX Snow Leopard, For Dummies”

Mathieu

08.10.2010 | 5:31 pm

This is awesome, Thank you!

Patrick

08.15.2010 | 5:51 pm

Great! indeed great! had been hammering my head against the wall all along trying this and that and regretting why I ever switched to mac where my degrees of freedoms are so fewer now…..

Thank you for this!

08.23.2010 | 5:36 pm

Not sure if you can help but these are the errors I get after starting my Terminal after setting it up as you mentioned.

Thanks
Joe Ryan

-bash: export: `=’: not a valid identifier
-bash: export: `–color=auto’: not a valid identifier
-bash: export: `=’: not a valid identifier
-bash: export: `=’: not a valid identifier

Leave a Reply

Green Web Hosting! This site hosted by DreamHost.