51

Certain commands produce text in color for readability.

I'm using Linux. For example when I'm using rak or hg diff the output is in color for better readability.

However when I pipe the output through less

hg diff | less

the colors are lost.

How do I preserve the color?

Thanks!

4

3 Answers 3

52

I believe some commands are smart enough NOT to output color if they detect that they are writing to a pipe or a file instead of to the console, since that could ruin the parsing of their output by the next program in the pipeline.

You can try forcing the programs into outputting color with their respective flags (e.g. --color or whatever), but it's ultimately implementation dependent if they'll honor your request or not.

GNU grep 2.27, for example, will not output color into less even when passing --color to it. But if you pass --color=always and pipe it into less, you'll be able to see the color escape codes through less. And then, using the -R flag will have less interpret the color escape codes.

2
  • 2
    And for git status, it is git -c color.status=always status | less -REX Commented May 26, 2020 at 14:41
  • thank you. I had found the answer to this years ago but obviously did not read enough. I had it in my mind that -R flag for less was the solution. After changing jobs, I found it wasn't working anymore and never bothered to look into it until now. Thanks for explaining you need to use the output colour on the preceeding command before less as well as using the -R flag for less! Commented Jul 8, 2022 at 10:42
23

Try less -r or (safer) less -R. See the manual.

Since you probably don't want to specify that all the time:

export LESS=-R # Put that in a startup script like .bashrc.local
hg diff | less

For Mercurial, you can also use the pager extension.

Note: Some commands automatically turn of color output when they detect the output goes to a pipe instead of the terminal. To fix this, force color output.

For example ls -al will show color output but ls -al | less -R will not. ls -al --color | less -R will work as expected.

4
  • 1
    Thank you. But this doesn't seem to work for me for some reason. I'm on Linux Fedora.
    – Jon Snow
    Commented Nov 8, 2012 at 17:22
  • I'm on Ubuntu and it works here. Does man less show the option? Commented Nov 9, 2012 at 8:40
  • 1
    @ZackXu you also need to use the output color option on the command you're piping into less. Commented Jul 8, 2022 at 10:43
  • @ZackXu Check the options of the command that you pipe to less. Some commands detect that the output is a pipe and not a terminal and decide to use plain text in this case. Commented Jul 11, 2022 at 18:16
-1

I also discovered color options on grep, for instance, --color[=never|=auto|=always] affect what happens to coloring. =auto will see the pipe and remove colors. =always will pass the codes on through and less -R will properly render them as discussed above.

1
  • And this improves on the grep related section of the 5 year old answer how exactly?
    – tink
    Commented Nov 21, 2023 at 22:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.