Howto colorize Unix/Linux stuff

Since SOOO many people ask me this ... and don't know how to more specifically ask this, here is (most likely) what you are looking for. In this howto, code lines beginning with a dollar sign ($) do so to indicate they are command-line entries. You should not enter that dollar sign. I assume you already know how to edit a file. If you don't, use pico (console) or gedit (gui), they're easy to pick up.

If your shell is tcsh, do chsh and enter /bin/bash for your new shell, then log off and come back. If don't like bash, try /bin/zsh. If you actually like tcsh, use setenv VARIABLE VALUE in place of export VARIABLE=VALUE.

You need a remote control file. For simplicity, we're going to make your profile and your rc the same. In bash, do this:

$ cd
$ touch .bashrc
$ ln -s .bashrc .bash_profile

CCIS UNIX ONLY: You also need to be using the GNU tools (they are more sane and up-to-date). Edit your ~/.software file and make sure the top one or two entries (not counting # comments) are some combination of "GNU" and/or "beta" ... the only one you really need is GNU. Good examples can be found at ~katz/.software, ~bass/.sofware, and ~muncus/.software (using the "if you can read it, steal it" mentality on people aware of it and know how to prevent it). After the edit, type resoft and your software environment is updated. A test of which grep should return /arch/gnu/bin/grep.

Colors in XTerm/Aterm (do this first!)

Do export TERM=dtterm on Unix or export TERM=xterm in Linux/*BSD/OSX/Cygwin (some people use xterm-color). Put this command in your ~/.bashrc file.

For terminal windows launched via Unix: write the following to ~/.Xdefaults:

! Aterm customized defaults
Aterm*reverseVideo: true
Aterm*visualBell: true
! for unix only (otherwise, use xterm or xterm-color)
Aterm*termName: dtterm
Aterm*saveLines: 5000
Aterm*transparent: true
Aterm*transpscrollbar: true
Aterm*shading: 40
Aterm*font: -*-clean-medium-r-*--14-*-*-*-*-60-*-*
! blue is usually too dark to see.  this fixes that
Aterm*color4: RoyalBlue
Aterm*color12: RoyalBlue

! Xterm customized defaults
XTerm*reverseVideo: true
XTerm*visualBell: true
XTerm*termName: dtterm
XTerm*saveLines: 5000
XTerm*scrollBar: true
XTerm*font: -*-clean-medium-r-*--14-*-*-*-*-60-*-*
XTerm*color4: RoyalBlue
XTerm*color12: RoyalBlue

After editing that file, run xrdb ~/.Xdefaults and open a new aterm (xterm can have problems on some systems not using XFree86/X.Org, and aterm has transparancy). You should have a pretty terminal window. CCIS ONLY: Test color support by running /proj/crew/bin/ansimodes ... this gives you every color combination available.

If that doesn't work

You are likely launching your terminal with some arguments. Don't do that. If you do not think you are doing that, check with alias aterm (or xterm, whatever). You can undo an alias with unalias aterm. Don't forget to run xrdb as above.

Colorize directory listings (LS)

Try alias ls='ls -h --color=auto' and then run ls -a ... we'll add this permanantly in the titlebar section below.

Colorize vim

Vim uses a file called ~/.vimrc (notice a trend?) to pull in settings. Unlike most rc files, vim's uses a double-quote for comments (instead of hash). You probably want something like this:

" settings for vim
syntax on
set backround=dark
set ruler

Again, for people who know the concept, "if you can read it, steal it:" ~katz/.vimrc, ~bass/.vimrc, ~muncus/.vimrc. And use vim instead of vi. If you need help remembering, add a line like alias vi='vim' to your ~/.aliases.

Colorize grep

Quick-and-Dirty: grep --color. 1;32 is green, default is red; see the ansimodes command above or remove the GREP_COLOR part. A better way to do it is to add this to your ~/.bashrc (or as shown in the titlebar section below):

if echo hello|grep --color=auto l >/dev/null 2>&1; then
  export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
fi

Colorize prompt

Try export PS1='\[\033[0;32m\]\u@\h \w\$ \[\033[0;38m\]' and if you like it, we'll add this permanantly in the titlebar section below.

If you want to get more advanced, there are volumes upon volumes of tricks you can add here, like using a different color for the working directory and showing the $ prompt as red when a command fails: export PS1='\[\033[0;32m\]\u@\h \[\033[0;33m\]\w\[\033[${?/[^0]/31}m\]\$ \[\033[0;38m\]' (test this by running the commands true and false).

Here is a hastily converted version of my zsh prompt configuration to bash, as a snippit of a potential .bashrc:

PSC() { echo -ne "\[\033[${1:-0;38}m\]"; }
[ -n "$CYGWIN" ] && cygpwd="s:^/cygdrive/([a-z])(/|$):\1\:/:;"
function PWD() { echo "$*" |perl -pne \
  's:^'"$HOME"':~:;'"$cygpwd"'s:^(.{10}).{4}.*(.{20})$:$1...$2:;'
}
PR="0;32"		# default color used in prompt is green
if [ "$(id -u)" = 0 ]
  then sudo=41		# root is red background
  elif [ "$(id -un)" != "$(basename $HOME)" ]
    then sudo=31	# not root, not self: red text
  else sudo="$PR"	# standard user color
fi
alias PS1='export PS1="\u@\h \w\$ "'	# if in trouble, run the command 'PS1'
PROMPT_COMMAND='[ $? = 0 ] && PS1=${PS1[1]} || PS1=${PS1[2]}'
PSbase="$(PSC $sudo)\u$(PSC $PR)@\h $(PSC 33)\$(PWD \w)"
PS1[1]="$PSbase$(PSC $PR)\$ $(PSC)"
PS1[2]="$PSbase$(PSC  31)\$ $(PSC)"
PS1="${PS1[1]}"
unset sudo PR PSbase

Note how the directory-parsing call $(PWD \w) (only) is escaped. This is because the shell needs to recalculate these parts on every new line, whereas the rest is static. We use $PROMPT_COMMAND to switch between alternate prompts based on the return value of the most recent command. If the above were saved into snippit.bashrc, we could test it like this:

$ source snippit.bashrc adam@sandbox ~$ false adam@sandbox ~$ sudo -s # source snippit.bashrc root@sandbox ~# exit adam@sandbox ~$ sudo -u bob -s $ source snippit.bashrc bob@sandbox ~$ false bob@sandbox ~$ exit adam@sandbox ~$ cd /tmp/reallylongpath/that/just/keeps/going/and/going adam@sandbox /tmp/reall...eeps/going/and/going$ _

Better Titlebar Reporting

Create a ~/.titlebar file or something and put this in it:

# determine if okay to set colors and termname then does so
# checks copied from default redhat installation /etc/bashrc

# are we an interactive shell?
if [ "$PS1" ]; then
    if [ -x /usr/bin/tput ]; then
      if [ "x`tput kbs`" != "x" ]; then # We can't do this with "dumb" terminal
        stty erase `tput kbs`
      fi
    fi
    case $TERM in
      *xterm* | dtterm | *vt100* | *linux* | *cyg* )
        [ `uname -s` = "SunOS" ] && TERM=dtterm || TERM=xterm
        HOST=`hostname |sed 's/\..*$//g'`
        PROMPT_COMMAND='echo -ne "\033]0;${LOGNAME}@${HOST}: ${PWD}\007"'
        PS1='\[\033[0;32m\]\u@\h \w\$\[\033[0;38m\] '
        if echo hello|grep --color=auto l >/dev/null 2>&1; then
          export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
        fi
        if ls --help 2>&1 |grep color >/dev/null; then
          alias ls='ls -h --color=auto'
        fi
        ;;
      *)
        PS1='\u@\h \w\$ '
        ;;
    esac
fi

Then add a source ~/.titlebar command to your ~/.bashrc and you're all set. Notice that I've moved all of the environment and alias items to this file so that all the checking for a good terminal is done at once and in a sane manner. This is the way to go.

Set background image

To set a background image, there are several simple commands you can utilize, such as wmsetbg -s imagefile or bsetbg imagefile -- take a look at the man pages for each in order to get the display working. You can get good images at Digital Blasphemy or Astronomy Picture of the Day archive.

More info...

For more information, try the man pages, the --help flag, the home directories of katz, bass, and muncus, the CCIS Crew Leet Skilz Talk's slides, or google.

[adam katz] [games] [hacking] [politics] [weather] [links]

W3C | XHTML 1.0     W3C | CSS

All content copyright © 2003-04 by Adam Katz unless otherwise noted. All rights reserved. Page last updated Wed Nov 10 19:45:21 2004.