Getting colored results when using a pipe from grep to less

249

71

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 

EDIT:

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

Jeremy Powell

Posted 2009-09-04T21:46:30.363

Reputation: 5 419

possible duplicate of Get colors in 'less'' command

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2014-09-19T08:07:24.517

What does * do? From the man page of grep: *: The preceding item will be matched zero or more times. But I still don't understand..! @JeremyPowell – Shayan – 2019-09-01T17:37:49.063

1@Shayan, the '*' in this case is for the file arguments. It gets processed by the shell which expands it to all files in the directory. The search string is enclosed in double quotes in the example. – NeilG – 2019-11-02T04:15:46.717

Answers

277

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

Will always enable coloring and override the automatic detection, and you will get the color highlighting in less.

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

drrlvn

Posted 2009-09-04T21:46:30.363

Reputation: 4 695

@OwenBlacker the reason it 'just' works for you is probably because some linux distros setup some default aliases. Do alias less in a terminal and see what you get. – PhilT – 2014-07-23T16:23:59.117

1Using grep without -R and more instead of less -R works for me... and its shorter. In other words, this works on Fedora 20 grep --color=always -Irn "foo_bar" | more – Ray Foss – 2014-10-06T13:20:11.697

@spatz: You could integrate Dennis answer into yours to make it a one-stop-shop. Integration answers are perfectly wanted on SO. Thanks! That would solve the problem Owen addressed in his comment.

– cfi – 2015-09-09T08:17:21.383

2@OwenBlacker It might not be an alias. You might have $LESS set with -R. – greyfade – 2016-08-30T21:01:21.513

Ubuntu 16.04 cat file.log | grep --color=always password | less -R – Ligemer – 2018-03-22T22:59:58.213

To make less default to always pass through colors, edit your LESS environment variable to include -R or its long form --RAW-CONTROL-CHARS. The LESS variable contains a space-separated list of default flags for less. – Rory O'Kane – 2018-03-28T20:16:35.897

145You need to use less -R for the colour encoding to be interpreted by less correctly – therefromhere – 2009-09-04T23:24:17.013

1It worked for me with just less, it may be version dependent. – drrlvn – 2009-09-05T12:14:07.090

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 – 2009-09-08T14:16:44.037

Awesome. Though I too to use less -R to get less to display colours, rather than the colouring escape codes :o) – Owen Blacker – 2012-03-06T15:05:27.310

1A (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 – 2012-03-06T15:35:30.627

7I 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 – 2013-07-10T00:42:13.120

Yay, works for git log -p too. :) – David Winiecki – 2013-10-08T21:37:43.937

28

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

Paused until further notice.

Posted 2009-09-04T21:46:30.363

Reputation: 86 075

29Warning!: GREP_OPTIONS="--color=always" may break many scripts that use grep (or (e|f)grep). – mctylr – 2010-03-12T19:25:45.043

4Yeah, better to just alias grep. You can always get pure grep with GREP, or override the --color option manually. – asmeurer – 2011-07-18T22:51:45.677

This doesn't work for me, alias does work though. – saeedgnu – 2014-02-28T03:30:25.710

9

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:

#!/bin/sh
grep --color=always -r "$@" . | less -R

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

Iazel

Posted 2009-09-04T21:46:30.363

Reputation: 191

2Why not just use shell functions for this kind of thing? g() { grep --color=always -r "$@" . | less -R } works identically and probably will give (minutely) better performance. – 00dani – 2017-01-11T02:00:18.300

1@00dani yeah, that's a valid alternative too and sometimes I use it. Please note that in this case, most time is spent in IO and therefore there is no perceivable performance boost :) Another difference is that once in your PATH, this script can be used with other shell scripts and aliases too; the function instead needs to be explicitly loaded – Iazel – 2019-01-27T12:15:22.447

1

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

not2qubit

Posted 2009-09-04T21:46:30.363

Reputation: 1 234

3An alias is probably undesirable here. less supports a $LESS environment variable. So, instead of an alias, export LESS='-R' might be preferable. – greyfade – 2016-08-30T20:59:31.907

3Not 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 – 2013-04-17T18:35:36.680

3Note that less option -r is different than -R. Probably -R is safer. – Craig McQueen – 2014-01-09T00:26:36.017

1So why down-vote my solution. The OP specifically ask for less with the example already using `--color' option. – not2qubit – 2014-01-09T15:58:06.783

0

I need to run

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

with the -r flag after less, in order this to run.

Pierre François

Posted 2009-09-04T21:46:30.363

Reputation: 101

What does * do? From the man page of grep: *: The preceding item will be matched zero or more times. But I still don't understand..! – Shayan – 2019-09-01T17:36:53.533