Colors for gcc in shell
January 11th, 2008
I’ve always been annoyed by not having clearly marked errors (besides the word “error”) when using gcc/g++ from shell.
Now a simple python script helps me to notice every appearing error out of lots of unimportant lines of stuff (warnings e.g.).
highlight.py:
1 2 3 4 5 6 7 import sys while 1: input = sys.stdin.readline() if len(input.lower().split("error")) > 1: print chr(27) + '[91;1m' + input.strip() + chr(27) + '[0;0m' else: print input.strip()
Additionally I created a function and an alias in .bashrc which determines the behavior of the shell:
alias gcc_real=$(which gcc) alias gcc='gcc_highlight' function gcc_highlight() { gcc_real $@ 2>&1 | python ~/highlight.py }alias gpp_real=$(which g++) #...and the same for g++ }
The usage of gcc_real is very important, otherwise gcc would call gcc and so on…perfect recursion. ![]()
gcc errors are not reported over stdout but over stderr which means i had to use 2>&1 to forward them.





