Monday, February 25, 2008

LSCOLORS

As stated earlier, I came back to the problem of LSCOLORS.

If using bash, in order to get colorful directory listings in FreeBSD the user needs to set the CLICOLOR environment variable to "YES" and export it and would also need to define (and export) the LSCOLORS variable, all inside ~/.bash_profile.

So, how do we define LSCOLORS?

Here's a quick explanation: LSCOLORS is a string made up of 11 "FB" pairs, where F is the foreground color and B is the background color.

The available color codes are:

a -- black
b -- red
c -- green
d -- brown
e -- blue
f -- magenta
g -- cyan
h -- light gray
A -- bold black, usually shows up as dark gray
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 gray; looks like bright white
x -- default foreground or background

So now that we've got the color codes, all we need to do is to come up with suitable color pairs for each file type; below is an example of such an association:

DIR=Ex
SYM_LINK=Gx
SOCKET=Fx
PIPE=dx
EXE=Cx
BLOCK_SP=Dx
CHAR_SP=Dx
EXE_SUID=hb
EXE_GUID=ad
DIR_STICKY=Ex
DIR_WO_STICKY=Ex

Next, we want to assign the values above to the LSCOLORS variable and export it, therefore we need to specify:

export LSCOLORS="$DIR$SYM_LINK$SOCKET$PIPE$EXE$BLOCK_SP\
$CHAR_SP$EXE_SUID$EXE_GUID$DIR_STICKY$DIR_WO_STICKY"

And this settles it! Just remember to have somewhere in your ~/.bash_profile a line defining and exporting CLICOLOR as well:

export CLICOLOR="YES"

Logout, login again and you should be able to get your directory listings in color.

NOTE: Above, in the line with export LSCOLORS="...", I used an escape sequences ("\") in order to prevent the breaking of long lines. You, of course, should enter the whole line without ever using "\" and the [return] key.

7 comments:

  1. Thanks! Exactly what I was looking for!

    ReplyDelete
  2. thaks. works well on MAC OS X

    ReplyDelete
  3. I'm glad it does. It should be fairly similar, thought, as long as you use bash as the shell.

    ReplyDelete
  4. thanks for sharing this site. you can download lots of ebook from here

    http://feboook.blogspot.com

    ReplyDelete
  5. There's an LSCOLORS & LS_COLORS generator over here: http://geoff.greer.fm/lscolors/

    ReplyDelete
  6. @manicdee: That generator is very handy. Thank you for the hint!

    ReplyDelete
  7. Very Nice, but how i can apply colors for certain extensions, like png or gif files, like linux does *.png=34;40
    How can i do this on bsd or mac osx?
    thanks.

    ReplyDelete