Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to colorize output piped to more?

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?

like image 742
Kurtosis Avatar asked Apr 05 '12 00:04

Kurtosis


People also ask

Can colorized output be captured via shell redirect?

Redirecting doesn't strip colors, but many commands will detect when they are sending output to a terminal, and will not produce colors by default if not.

How do you show colors in less?

If you just want to tell less to interpret color codes, use less -R .

How do you grep with color?

The —-color parameter tells grep to color the search terms in the output, which helps them stand out when among all the other text on the line. You choose which color you want using the GREP_COLOR environment variable: export GREP_COLOR=36 gives you cyan, and export GREP_COLOR=32 gives you lime green.


2 Answers

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

like image 134
je4d Avatar answered Oct 22 '22 21:10

je4d


ls --color | less -r

Tested on Linux, GNU userland.

like image 27
jimw Avatar answered Oct 22 '22 21:10

jimw