71

I have a simple json file and if I pipe the output of "jq" into "less", the colors get removed.

This works:

# yey, lots of colors
jq "." /tmp/myfile.json

This doesn't work:

# ugly output :( , no colors
jq "." /tmp/myfile.json | less -R

Any ideas on how to make "less" keep the colors?

3
  • 7
    Did you try jq -C? Commented Jul 9, 2020 at 7:04
  • 2
    Why would I, if I see colored output by default?
    – filipg g
    Commented Jul 10, 2020 at 7:03
  • 7
    Because, as the answer added later below points out, jq does not by default add color output to a pipe. Commented Jul 11, 2020 at 5:15

1 Answer 1

109

It is jq that is suppressing the colours. From the man page

       o   --color-output / -C and --monochrome-output / -M:

           By default, jq outputs colored JSON if writing to a terminal.
You can force it to produce color even if writing to a pipe or a file
using -C, and disable color with -M

So, just use:

jq -C "." /tmp/myfile.json | less -R

and it will output colours regardless. The less command doesn't need the -R switch on my version, but I believe it does on older versions. If you see the ESC... codes, you will need that switch.

2
  • 2
    Thanks very much, exactly what's needed. jq -C "." /tmp/myfile.json | less -R
    – filipg g
    Commented Jul 10, 2020 at 7:01
  • That manpage first sentence is unclear, it would be more clear for the doc to say "By default, jq outputs colored JSON only if writing to a terminal, not a pipe or file"
    – smci
    Commented Nov 24, 2022 at 2:17

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