I'm compiling a file with gcc (actually I'm make'ing, but never mind), which sends some colorized error messages to stderr. So, I do:

gcc a.c 2>&1 | less

but what I get is lines such as:

a.c: In function <E2><80><98>whatever(int)<E2><80><99>:
a.c:11:10: warning: etc etc

that is, I see the numeric escape codes rather than the colors.

I tried to follow the advice in this answer on SU, but it didn't work for me. Or rather, ls --color | less works, but not my gcc command above. What should I do?


  • I'm connected to this machine via SSH, with TERM=xterm-256color
  • Without piping through less, I do get the colors on the terminal.
  • 1
    Does less -R work? – Wildcard Oct 7 '16 at 12:50
  • @Wildcard: No, in the sense that I get the same thing. – einpoklum Oct 7 '16 at 12:51
  • What if you pipe through cat? Do you still see colors on the terminal? – Wildcard Oct 7 '16 at 12:53
  • @Wildcard: No, I don't. but if I do cat -v I notice there are nonprinting characters. – einpoklum Oct 7 '16 at 12:56
  • Okay, that means the problem is with the escape sequences that gcc is generating when its output is sent somewhere other than a tty—rather than a problem with less. I don't have a gcc man page on the box I'm on right now, but if you search for "color" in your man page you might find something. Good luck. :) – Wildcard Oct 7 '16 at 13:00

Posting as an answer as requested.

There are two problems here:

  • By default gcc prints color codes only when the output is a terminal.
  • By default less doesn't send raw color characters to the terminal.

You can force gcc to print color codes by passing it the option -fdiagnostics-color=always. Beware that this will break various tools that rely on parsing compiler errors.

You can force less to send color codes to the terminal by passing it options -r or -R. The latter is safer, since it only allows codes for actual ANSI colors.

Summing it up:

gcc -fdiagnostics-color=always a.c 2>&1 | less -R

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.