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"
export CFLAGS="-fdiagnostics-color"
make 2>&1 | less -R 
  • 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 4 '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 4 '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 4 '15 at 17:20
  • Did you try lowercase option -r for less?
    – SΛLVΘ
    Nov 4 '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 4 '15 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.