Colorize Terminal – How to Colorize Your Terminal and Shell Environment

bashcolorspromptshell

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

What options exist to add color to my terminal environment? What tricks do you use? What pitfalls have you encountered?

Unfortunately, support for color varies depending on terminal type, OS, TERM setting, utility, buggy implementations, etc.

Here are some tips from my setup, after a lot 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 use everything from macOS X, Ubuntu Linux, RHEL/CentOS/Scientific Linux and FreeBSD. I'm trying to keep things simple and generic, if possible.
  3. I do a bunch of work using GNU screen, which adds another layer of fun.
  4. Many OSs set things like dircolors and by default, and I don't want to modify this on a hundred different hosts. So I try to stick with the defaults. Instead, I tweak my terminal's color configuration.
  5. Use color for some Unix commands (ls, grep, less, vim) and the Bash prompt. These commands seem to use the standard "ANSI escape sequences". For example:

    alias less='less --RAW-CONTROL-CHARS'
    export LS_OPTS='--color=auto'
    alias ls='ls ${LS_OPTS}'
    

I'll post my .bashrc and answer my own question Jeopardy Style.

Best Answer

Here are a couple of things you can do:

Editors + Code
A lot of editors have syntax highlighting support. vim and emacs have it on by default. You can also enable it under nano.

You can also syntax highlight code on the terminal by using Pygments as a command-line tool.

grep
grep --color=auto highlights all matches. You can also use export GREP_OPTIONS='--color=auto' to make it persistent without an alias. If you use --color=always, it'll use colour even when piping, which confuses things.

ls

ls --color=always

Colors specified by:

export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33'

(hint: dircolors can be helpful)

PS1
You can set your PS1 (shell prompt) to use colours. For example:

PS1='\e[33;1m\u@\h: \e[31m\W\e[0m\$ '

Will produce a PS1 like:

[yellow]lucas@ubuntu: [red]~[normal]$

You can get really creative with this. As an idea:

PS1='\e[s\e[0;0H\e[1;33m\h    \t\n\e[1;32mThis is my computer\e[u[\u@\h:  \w]\$ '

Puts a bar at the top of your terminal with some random info. (For best results, also use alias clear="echo -e '\e[2J\n\n'".)

Getting Rid of Escape Sequences

If something is stuck outputting colour when you don't want it to, I use this sed line to strip the escape sequences:

sed "s/\[^[[0-9;]*[a-zA-Z]//gi"

If you want a more authentic experience, you can also get rid of lines starting with \e[8m, which instructs the terminal to hide the text. (Not widely supported.)

sed "s/^\[^[8m.*$//gi"

Also note that those ^[s should be actual, literal ^[s. You can type them by pressing ^V^[ in bash, that is Ctrl + V, Ctrl + [.

Related Question