Color your shell – 1: colorful ls

colors for lsPreface:

This is a series of posts that explain how to get to a nicely colored shell by tweaking your .bahsrc.

I am aware that each of these hints has been discussed on the Internet forever. However, it took me a little while to get all into my .bashrc, so I hope this will be useful for someone.

Colorful directory listings (ls)

Getting color to ls is fairly easy. You basically just turn it on. However, depending on your OS, you may have to push different buttons to get it working.

For OS X, adding

CLICOLOR=1

to your .bashrc is already enough. I tested it for iTerm and the OS X Terminal and both worked like a charm.

For Linux, things are slightly more tricky because you need to tell ls that it should use colors.

Therefore, adding

CLICOLOR=1

to your .bashrc will only produce colorful directory listings if you do

ls --color=auto

This is somewhat inconvenient but luckily, there are aliases. Just add:

export CLICOLOR=1
alias ls='ls --color=auto'

to your .bashrc and you’re done.

Making the colors more colorful

If you find the colors too dark or too bright for your terminal settings, you may want to adjust the colors of the different file types.

For OS X you can simply do this by overwrituing the variable LSCOLORS with your own settings:

export LSCOLORS=dxfxcxdxbxegedabagacad

The ugly string indicates your color preferences. It is well explained in the manpages for ls (man ls). Just search for LSCOLORS.
I’ll add a brief explanation anyway:

The string is composed of pairs of color indexes (e.g., dx, fx, cx, dc, … in the example). Each letter is a color index. The first letter indicates the foreground color (d=brown) the latter the background color (x=default foreground or background = invisible). The position also matters. The first pair defines colors for directories, the second defines symlinks, etc. All this is explained quite neatly in the man pages so I hope you forgive me if I don’t repeat it all.

For Linux things work a bit different again (sigh!)

Here the variable is not LSCOLORS but LS_COLORS (great job, OS X and Linux guys!)

So you could set the following in your .bashrc

LS_COLORS='di=33:fi=0:ln=95:pi=5:so=5:bd=5:cd=5:or=37:mi=0:ex=31:*.rpm=90'

In contrast to OS X, the pairs are named here. di stands for directory, fi for file, ln for a symlink…
The numbers stand for colors and you can even add multiple numbers for one thing if you like to have underlined, flashing or otherwise fancy-looking colors.

If you want to delve deeper into the topic I suggest to follow this LINK. It explains the whole color concept quite well.

Making it work cross platform

If you intend to use the same .bashrc across Linux and Mac systems you might want to add some smartness to this.

Here is some example code. I think it is simple enough to go without further explanation:

# don't do this for dumb terminals
if [ "$TERM" != "dumb" ]; then
 if [ $(uname) == "Linux"  ]; then
   # Linux
   alias ls='ls --color=auto'
   LS_COLORS='di=33:fi=0:ln=95:pi=5:so=5:bd=5:cd=5:or=37:mi=0:ex=31:*.rpm=90'
 else
   # OS X   
   alias ls='ls -G'
   export LSCOLORS=dxfxcxdxbxegedabagacad
 fi
 #This is for everyone       
 export CLICOLOR=1
fi

I may have stolen parts of the above example code from here and there. I tried to figure out where it all came from but it is so basic that determinung the exact origin is impossible. If you feel that you deserve credit for it, please tell me and provide a link to your blog/post/whatever.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s