How do I keep colors when piping "jq" output to "less"? How do I keep colors when piping "jq" output to "less"? unix unix

How do I keep colors when piping "jq" output to "less"?


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 fileusing -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.