November 12, 2010

grep with less and color

When you simply run grep --color it implies grep --color=auto which detects whether the output is a terminal and if so enables colors. However, when it detects a pipe it disables coloring. The following command:
grep --color=always -R "search string" * | less -R
Will always enable coloring and override the automatic detection, and you will get the color highlighting in less.

You can add these to your bashrc aliases:

alias grep='grep --color=always'
alias less='less -R'

1 comment:

  1. Perfect. Solution. Grep coloration appears in 'less' output just as in terminal. Thanks.