16

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
  • 161
  • 1
  • 3

2 Answers2

15

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
  • 251
  • 2
  • 3
  • Thanks for this. It works well for me. Note though that if you want to restrict this to strictly ANSI escapes, you use `less -R` rather than `less -r`. I had to look up what `|&` does. (https://stackoverflow.com/questions/35384999/what-does-mean-in-bash) – mc0e Sep 03 '23 at 04:47
6

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
  • 553
  • 3
  • 13
  • 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 Nov 04 '15 at 16:44
  • 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 Nov 04 '15 at 17:09
  • 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 Nov 04 '15 at 17:20
  • Did you try lowercase option `-r` for `less`? – SΛLVΘ Nov 04 '15 at 19:33
  • @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 Nov 04 '15 at 20:58
  • 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 Nov 05 '15 at 08:21
  • 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 Apr 23 '17 at 00:02