Take the 2-minute tour ×

I use the --colour option of grep a lot, but I often use less as well. How can I pipe grep results to less and still preserve the coloring. (Or is that possible?)

grep "search-string" -R * --colour | less


I'm looking for a direct solution or anything equivalent to this.

share|improve this question
add comment

5 Answers

up vote 92 down vote accepted


grep --color=always -R "search string" * | less


EDIT: Although using just less works for me, perhaps older version require the -R flag to handle colors, as therefromhere suggested.

share|improve this answer
You need to use less -R for the colour encoding to be interpreted by less correctly –  therefromhere Sep 4 '09 at 23:24
It worked for me with just less, it may be version dependent. –  spatz Sep 5 '09 at 12:14
wow. I thought 'auto' only depended on the terminal type. I may be jumping the gun, but this may revolutionize the way I use linux :P –  Jeremy Powell Sep 8 '09 at 14:16
A (hopefully) useful addendum: I needed to exclude some matches but maintain the colouring, so I actually ended up with grep pattern file | grep -v badpattern | grep --colour=always pattern | less -R, which met my needs perfectly. (Thanks again!) –  Owen Blacker Mar 6 '12 at 15:35
I can't believe they just implemented the color-handling feature in less and therefore missed the chance to have a special colorless tool. For the name alone it should have been done! I should probably write a patch that invokes -R automatically when the binary is run as colorless. –  Christian Jul 10 '13 at 0:42
show 2 more comments

You can put this in your .bashrc file:

export GREP_OPTIONS="--color=always"

or create an alias like this:

alias grepc="grep --color=always"

and you will need to use the -R option for less, as pointed out by therefromhere

share|improve this answer
Warning!: GREP_OPTIONS="--color=always" may break many scripts that use grep (or (e|f)grep). –  mctylr Mar 12 '10 at 19:25
Yeah, better to just alias grep. You can always get pure grep with GREP, or override the --color option manually. –  asmeurer Jul 18 '11 at 22:51
This doesn't work for me, alias does work though. –  ilius Feb 28 at 3:30
add comment

Don't alias "grep", better to alias "less" which is never used by shells. In your .bashrc just put: alias less="less -r".

share|improve this answer
Not quite right. One needs to use both grep --color=always and less -R. Note that grep only knows it is being piped into some other process and the --color=auto option uses solely this information to decide if will output colors or not. –  brandizzi Apr 17 '13 at 18:35
Note that less option -r is different than -R. Probably -R is safer. –  Craig McQueen Jan 9 at 0:26
So why down-vote my solution. The OP specifically ask for less with the example already using `--color' option. –  user1147688 Jan 9 at 15:58
add comment

In case like this, I prefer to actually create small sh files and put them on /usr/local/bin.
I usually use grep in the recursive way on the pwd, so thats my personal script:

grep --color=always -r "$@" . | less -R

And then I've just copied it as /usr/local/bin/g (yes, I use it a lot)

share|improve this answer
add comment
 # just the combine the best answer with find ...   
 find $dir -type f -exec grep -nHP --color=always -R "$to_srch" {} \; | less -R
share|improve this answer
add comment

Your Answer


