How to preserve coloured output when piping to `less -R`

colorslesspipepty

$ ffmpeg -v debug ... 

Coloured output.

$ ffmpeg -v debug ... |& less -R

Dull output.

How do I make the output coloured while piping it to something?

Best Answer

For commands that do not have an option similar to --color=always, you can do, e.g. with your example:

script -c "ffmpeg -v debug ..." /dev/null < /dev/null |& less -R

What script does is that it runs the command in a terminal session.

EDIT: Instead of a command string, if you want to be able to provide an array, then the following zsh wrapper script seems to work:

#!/usr/bin/env zsh
script -c "${${@:q}}" /dev/null < /dev/null |& less -R