17 Nov 2009

Linux Tip: Color enabled pager - less

Recently I was using a command line tool which was generating many lines of color text. The output was displayed so fast on my xterm, that I couldn't read it. So I thought, that I could use "| less" pager to see what's up, and I was wrong :( - less "out of the box" doesn't support colors. I've tried most pager but I prefer less.

... but there is a way!

Less doesn't support colors "as it is", but there are some hacks. Thanks rha7dotcom.
export LESS="-RSM~gIsw"
  • R - Raw color codes in output (don't remove color codes)
  • S - Don't wrap lines, just cut off too long text
  • M - Long prompts ("Line X of Y")
  • ~ - Don't show those weird ~ symbols on lines after EOF
  • g - Highlight results when searching with slash key (/)
  • I - Case insensitive search
  • s - Squeeze empty lines to one
  • w - Highlight first line after PgDn
Remember the tip with export LESS works only if you software you want to page uses RAW ASCII colors not those ncursed based!

Color man pages using less pager

Thanks Nion
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
To make it available for full time add this entries to your ~/.bashrc or ~/.${SHELL}rc. Hope this helps someone.

3 comments:

  1. I've found that colour highlighting on console can be difficult :(

    If you want to color src preview via less, do something like:

    alias vless='/usr/share/vim/vimcurrent/macros/less.sh'

    and use vless instead less.

    If you don't like vim, you can try to use tip provided by Peter Eisentraut (command line syntax highlight using source-highlight, or command line syntax highlighting using highlight)

    ReplyDelete
  2. Thanks a million!!!
    I've been looking for this for such a long time! :D

    ReplyDelete
  3. If you are courious what all this stuff means:

    export LESS_TERMCAP_mb # begin blinking
    export LESS_TERMCAP_md # begin bold
    export LESS_TERMCAP_me # end mode
    export LESS_TERMCAP_se # end standout-mode
    export LESS_TERMCAP_so # begin standout-mode - info box
    export LESS_TERMCAP_ue # end underline
    export LESS_TERMCAP_us # begin underline

    btw: less with syntax hilighting: alias vless='/usr/share/vim/vim??/macros/less.sh'

    or use: source highlight software: http://www.gnu.org/software/src-highlite

    ReplyDelete