Is it possible to colorize output piped to more?

Go To StackoverFlow.com

8

I have ls and grep aliased to 'ls --color=auto' and 'grep --color=auto' for colorized output, but when I pipe to more the color is lost.

Neither more nor less seems to have a param for colorizing their output. Is there any way to do this?

2012-04-05 00:56
by Kurtosis


16

The problem isn't that more and less aren't colourizing their output, it's that ls is not outputting the colour because it's connected to another process rather than the terminal.

You can't easily get ls to be any smarter about when it outputs colour, but you can add --color to force it to output colour when you're piping it to more

When you have colour output, use ... |less -R to make less pass the colours through to the terminal instead of showing the escape codes as text

2012-04-05 00:59
by je4d
Ideally you want less -R rather than less -r - jørgensen 2012-04-05 05:06
thanks @jørgensen, I just re-read the man page and agree. updated above - je4d 2012-04-05 06:52
If you're on a Mac (OS X 10.9 at least) you'll have to use --color=always (for grep), CLICOLOR_FORCE=true env variable for ls etc. to get the commands to spit out color codes even when the receiver is not a terminal - Sebastian Ganslandt 2014-03-20 11:50


3

ls --color | less -r

Tested on Linux, GNU userland.

2012-04-05 00:59
by jimw
Thanks, that works too. I'd give you the green check too if I could, but je4d beat you by just a few seconds. Thanks - Kurtosis 2012-04-05 01:22
Thanks for the thanks :-) je4d's answer is also much more comprehensive, so IMO he deserves the tick - jimw 2012-04-05 01:24
Ads