jeudi 4 septembre 2008

Pimp my grep

I'm in a situation where i use a lot of grep.
I recently discovered grep allows much more results coloration than i previously thought.

There's the obvious :

alias grep ='grep --color=auto' which will color matches in red by default.

But there's a GREP_COLORS environment variable that allows to configure file name coloring, line number coloring, context coloring and others...

Just add:

export GREP_COLORS='mt=01;31:ml=:cx=:fn=36:ln=32:bn=32:se=36'

to your .bash_profile or adjust accordingly to your $SHELL.


syntax for this variable is avaible in the man page, mt=matching text,cx=context, ml=matching lines, ... numbers are term colors . Much easier to read when going through a lot of matches :)

The GREP_COLOR env var is deprecated but still honored, and allows to specify matching text color only (thus less powerful that the new GREP_COLORS above).

Its syntax is just a term color , i.e : GREP_COLOR="01;33"
Although it has been deprecated , If you use a version of grep older than 2.5.2 / 2.5.2 you might need it...
You can get an array of available colors here

Just a little screenshot of it :

Aucun commentaire: