Keep color in less after make and pipe

I would like to keep color in less command after using "make 2>&1" to compile some program. There are similar topics with "grep" and "ls" commands but solutions do not work with this command.

For instance,

make 2>&1 | less -R 

does not work.

Thanks for your help.


The simplest solution is:

unbuffer make |& less -r

This is based on the answer to Preserve colors while piping to tee

I had to "sudo apt-get install expect" to get the unbuffer command installed.

Note that the "-r" option for less tells it to display ANSI color codes, while using |& pipes in both STDOUT and STDERR.


I think that you need to pass special parameters to gcc for this. Try this and let me know if it works:

export CXXFLAGS="-fdiagnostics-color"
#or
export CFLAGS="-fdiagnostics-color"
make 2>&1 | less -R