6 of 10
updated grep to deal with `$GREP_OPTIONS`, colors for `perldoc`
Adam Katz
  • 3.6k
  • 1
  • 24
  • 30

Colors for man pages (more detail):

function _colorman() {
  env \
    LESS_TERMCAP_mb=$(printf "\e[1;35m") \
    LESS_TERMCAP_md=$(printf "\e[1;34m") \
    LESS_TERMCAP_me=$(printf "\e[0m") \
    LESS_TERMCAP_se=$(printf "\e[0m") \
    LESS_TERMCAP_so=$(printf "\e[7;40m") \
    LESS_TERMCAP_ue=$(printf "\e[0m") \
    LESS_TERMCAP_us=$(printf "\e[1;33m") \
      "$@"
}
function man() { _colorman man "$@"; }
function perldoc() { command perldoc -n less "$@" |man -l -; }

Colors for grep (1;32 is bright green, see other posts here for other colors):

GREP_COLOR='1;32'         # bright green rather than default red
GREP_OPTS='--color=auto'  # for aliases since $GREP_OPTIONS is deprecated
alias   grep='grep $GREP_OPTS'
alias egrep='egrep $GREP_OPTS'
alias fgrep='fgrep $GREP_OPTS'

More colors for GNU ls:

# use the config at ~/.dircolors if it exists, otherwise generate anew
eval "$( dircolors --sh $(ls -d ~/.dircolors 2>/dev/null) )"

# Usage: _ls_colors_add BASE NEW [NEW...]
# Have LS color given NEW extensions the way BASE extension is colored
_ls_colors_add() {
  local BASE_COLOR="${LS_COLORS##*:?.$1=}" NEW
  if [ "$LS_COLORS" != "$BASE_COLOR" ]; then
    BASE_COLOR="${BASE_COLOR%%:*}"
    shift
    for NEW in "$@"; do
      if [ "$LS_COLORS" = "${LS_COLORS#*.$NEW=}" ]; then
        LS_COLORS="${LS_COLORS%%:}:*.$NEW=$BASE_COLOR:"
      fi
    done
  fi
  export LS_COLORS
}

_ls_colors_add zip xpi jar
_ls_colors_add ogg opus

alias ls="ls -ph --color=auto"

Install grc (Generic Colouriser) and add it to your aliases:

# using this as a variable allows easier calling down lower
export GRC='grc -es --colour=auto'

# loop through known commands plus all those with named conf files
for cmd in g++ head ld ping6 tail traceroute6 `locate grc/conf.`; do
  cmd="${cmd##*grc/conf.}"  # we want just the command
  # if the command exists, alias it to pass through grc
  type "$cmd" >/dev/null 2>&1 && alias "$cmd"="$GRC $cmd"
done

# ./configure needs special handling: does it exist and is it executable?
alias configure="[ -x ./configure ] && $GRC ./configure"
    
# GRC plus LS awesomeness (assumes you have an alias for ls)
unalias ll 2>/dev/null
if ls -ld --color=always / >/dev/null 2>&1; then GNU_LS=true; fi
function ll() {
  local color= CLICOLOR_FORCE
  if [ -t 1 ] || [ "$CLICOLOR_FORCE" = true ]; then
    if [ -n "$GNU_LS" ]; then color="--color=always"; fi
    CLICOLOR_FORCE=true
  else
    CLICOLOR_FORCE=
  fi
  $GRC `alias ls |awk -F "'" '{print $2}'` -l $color ${1+"$@"}
}

Colors for diff: Too much content for a function, use a script and alias it in your rc file (unnecessary if you installed grc):

#!/usr/bin/perl
use strict;
use warnings;

open (DIFF, "-|", "diff", @ARGV) or die $!;

my $ydiff = 1;
while (<DIFF>) {
  if (not -t 1) {
    print;
    next;
  }
  chomp;
  $ydiff = 0 if /^[ <>\@+-]/ or ($. == 1 && /^\d+[a-z]{1,5}\d+$/);
  my $color = "";
  if (! $ydiff && /^[\@+-<>]/) {
    $color = (/^[<-](?!--$)/ ? 1 : /^[+>]/ ? 2 : 5);
  } elsif ($ydiff && /\t {6}([<|>])(?:\t|$)/) {
    $color = ($1 eq "<" ? 1 : $1 eq ">" ? 2 : 4);
  }
  $color ? printf ("\e[1;3%dm%s\e[0;0m\n",$color,$_) : print "$_\n";
}
close DIFF;

Colors for bash prompt:

# Shorten home dir, cygwin drives, paths that are too long
# (just bourne; no perl, sed, or bashisms!)
function PSWD() {
  local p="$*" parta partb
  if [ "$p" = "${HOME:-empty}${p#$HOME}" ]
    then p="~${p#$HOME}"
  fi
  # Fix Cygwin drive designations
  if [ "$OS" != "${OS#*CYGWIN}" -a "${p#/cygdrive}" != "$p" ]; then
    p="${p#/cygdrive/}"
    parta="${p%%/*}"
    if [ -z "$parta" ]
      then p="$p:"
      else p="$parta:${p#?}"
    fi
  fi
  # if the resulting path is 34+ characters, truncate it
  parta="${p#??????????????????????????????????}"
  if [ "${parta:-$p}" != "$p" ]; then
    parta="${p#??????????}"     # the path, minus the first 10 chars
    parta="${p%$parta}"         # the first 10 chars of the path
    partb="${p%????????????????????}"   # the path, minus the last 20 chars
    partb="${p#$partb}"         # the last 20 chars of the path
    p="$parta...$partb"         # 10 chars plus 3 dots plus 20 chars = 33
  fi
  echo "$p"
}

PSC() { echo -ne "\[\033[${1:-0;38}m\]"; }
PR="0;32"       # default color used in prompt is green
if [ "$(id -u)" = 0 ]; then
    sudo=41     # root is red background
  elif [ "$USER" != "${SUDO_USER:-$USER}" ]; then
    sudo=31     # not root, not self: red text
  else sudo="$PR"   # standard user color
fi
PROMPT_COMMAND='[ $? = 0 ] && PS1=${PS1[1]} || PS1=${PS1[2]}'
PSbase="$(PSC $sudo)\u$(PSC $PR)@\h $(PSC 33)\$(PSWD \w)"
PS1[1]="$PSbase$(PSC $PR)\$ $(PSC)"
PS1[2]="$PSbase$(PSC  31)\$ $(PSC)"
PS1="${PS1[1]}"
unset sudo PR PSbase

demo of bash prompt

Adam Katz
  • 3.6k
  • 1
  • 24
  • 30