21 September, 2010

Color highlighting in less

To enable syntax highlighting in less, place this script in ~/.lessfilter and make it executable.
#!/bin/bash

ext="${1##*.}"
name="${1%.*}"
#[ "${name##*.}" == "$name" ] || ext="${name##*.}.$ext"

# enscript --color --language=ansi --highlight=eiffel -o - -q "$1"

if [ -n "$(grep "\(\*\.$ext)\)\|\(\*\.$ext|\)" /usr/bin/lesspipe)" ]; then
  # echo handle $ext by lesspipe"
  exit 1
else
  # echo "hadle $ext by source-highlight"
  source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i "$1"
  exit 0
fi
and than export LESSOPEN, LESSCLOSE and LESS in your ~/.profile:
# use highligting in less
export LESSOPEN="| /usr/bin/lesspipe %s";
export LESSCLOSE="/usr/bin/lesspipe %s %s";
export LESS='-R --tabs 2'

No comments: