Keep color in less after make and pipe

7

1

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.

fylou

Posted 2015-11-04T15:41:59.500

Reputation: 71

Answers

7

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.

Digicrat

Posted 2015-11-04T15:41:59.500

Reputation: 171

3

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 

cristi

Posted 2015-11-04T15:41:59.500

Reputation: 453

This is a first step to a solution. The makefile contains gcc command and the option "-fdiagnostics-color" is necessary to keep color after gcc and pipe. Nevertheless, color is not kept after make and pipe. Even if I use a simple Makefile with only one "gcc" command inside and nothing else. – fylou – 2015-11-04T16:44:34.200

OK I understand the problem. Your solution works partially. Your CFLAGS can be overriden by any makefile. What I would like is to use a shell alias in the makefile. alias g++="g++ -fdiagnostics-color=always" is not enough or to know a cleaner solution. – fylou – 2015-11-04T17:09:10.797

A partial solution is to use make -e which keep variables taken from the environment precedence over variables from makefiles. It is not convenient in my case. – fylou – 2015-11-04T17:20:07.260

Did you try lowercase option -r for less? – SΛLVΘ – 2015-11-04T19:33:25.827

@fylou Why is the alias not a good enough solution for you? It looks like anything else will be a worse hack then this. – cristi – 2015-11-04T20:58:01.373

less -r doesn't not work.Aliases are not taken into account in the makefile. It would be ok for me if I could change this behaviour. – fylou – 2015-11-05T08:21:07.083

Assume that make output is colorized when not piped, on other commands (grep, ls, etcl) you add --colour=always (vs default auto). Make doesn't have this option so ? – Gerry Gleason – 2017-04-23T00:02:56.547