How do I preserve coloured output when piping to `less -R`?

$ ffmpeg -v debug ... 

Coloured output.

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

Dull output.

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

Asked By: Vi.

||

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
Answered By: vinc17

As alternative to script, which expectes command line as a string instead of normal array, there is a special mode of reptyr:

reptyr -L ffmpeg -v debug ... < /dev/null |& less -R

Note that not all versions of reptyr have working -L option and accept < /dev/null gracefully. See my pull request for this.

Answered By: Vi.
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.