Colorizing your terminal and shell environment?

Posted by Stefan Lasiewski on Super User See other posts from Super User or by Stefan Lasiewski
Published on 2010-05-28T03:43:00Z Indexed on 2010/05/28 3:53 UTC
Read the original article Hit count: 275

I spend most of my time working in Unix environments and using Terminal emulators. I try to use color on the commandline, because color makes the output more useful and intuitive.

What are some good ways to add color to my terminal environment? What tricks do you do? What pitfals have you encountered?

Unfortunately, support for color is wildly variable depending on terminal type, OS, TERM setting, utility, buggy implementations, etc. Here's what I do currently, after alot of experimentation:

  1. I tend to set 'TERM=xterm-color', which is supported on most hosts (but not all).
  2. I work on a number of different hosts, different OS versions, etc. I'm trying to keep things simple and generic, if possible.
  3. Many OSs set things like 'dircolors' and by default, and I don't want to modify this everywhere. So I try to stick with the defaults. Instead tweak my Terminal's color configuration.
  4. Use color for some unix commands (ls, grep, less, vim) and the Bash prompt. These commands seem to the standard "ANSI escape sequences"

I've managed to find some settings which are widely supported, and which don't print gobbledygook characters in older environments (even FreeBSD4!) (For the most part). From my .bash_profile

### Color support
# The Terminal application typically does 'export TERM=term=color'
# Some terminal types will print Black, White & underlined with these settings.

OS=`uname -s`

case "$OS" in
    "SunOS" )
        # Solaris9 ls doesn't allow color, so use special characters instead.
        LS_OPTS='-F'
        ;;
    "Linux" )   # GNU tools supports colors! See dircolors to customize colors
        export LS_OPTS='--color=auto'
        # Color support using 'less -R'
        alias less='less --RAW-CONTROL-CHARS'
    alias ls='ls ${LS_OPTS}
        export GREP_OPTIONS="--color=auto"
        ;;
    "Darwin"|"FreeBSD") # Most FreeBSD & Apple Darwin supports colors
        # LS_OPTS="-G"
        export CLICOLOR=true
        alias less='less --RAW-CONTROL-CHARS'
        export GREP_OPTIONS="--color=auto"
        ;;
esac

© Super User or respective owner

Related posts about command-line

Related posts about unix