Tiny Code

Tiny Code


Adding color to grep output

Most of the longtime Unix users like me love grep. Regular expressions make the silly wildcards available in Windows seem completely underwhelming.

One thing which I find makes grep even more useful is to configure it to highlight matching terms. Like most things I decide would be useful, a little poking about in the man pages shows that the Unix developers have anticipated my needs yet again. They've added a couple environment variables which can tell grep to highlight matching terms in the color of your choosing. Since I prefer my xterm windows with a dark background and light foreground colors, I find the following colors work well. Note - I use tcsh at home so you may need to alter these commands a bit to get them to work with your preferred shell.

# highlight search terms with a red background and white foreground
setenv GREP_COLOR '01;37;41'
setenv GREP_OPTIONS '--color=auto'

A word of warning is in order. I've found that using these terms can cause strange failures in seemingly unrelated activities. For example, I build Linux kernel modules at work and I've discovered that fails if I've got the grep color environment variables set (in the modpost step). So I've defined command aliases to set and unset the environment variables. That way I can easily enable grep colors before performing a grep and disable them again before doing something which may fail if grep colors are enabled.


Laura E. Goodin said...

It took me five or six times looking at that post to remember what "grep" means. And yes, I have worked with Unix, albeit briefly and at a very shallow level.

I am ashamed.

Chard said...

It's just another of those fine acronyms that Unices are famous for.

I will have to try that feature, though. Color is cool. I really like the addition of color to the output of "ls", for example. Very helpful. Reminds me of a utility that our host here supplied me with years ago on DOS, which did much the same thing.

BTW, Rod: Windows are so cheap these days, that if I need different environment variables for different purposes, I just put them in different windows (or tabs, if I'm using Konsole or something similar). I tend to have one window where I just run builds (ant, make, script, whatever), and others where I do all my editing, grepping, etc.

So I'll have a little script that sets the appropriate environment variables for a particular task, and just use it in the right window. It's a little tricky at first, but between saved sessions and ingrained habits, it's not too bad.

Rod said...

Back in the old days using a Sun workstation, I had fixed windows for each specialized task and took pains to save the layout so X opened up my carefully configured setup each time I logged in.

These days I'm not nearly as picky about my work environment. Anytime I need to do something in a directory where I may need again in the future, I open a new xterm window. I do have lots of scripts and command aliases to set up terminal windows for specific tasks though.

I count on virtual desktops to help keep things organized. What I'd really like is multiple LCDs hooked up to my development system. Ever since I saw Al Gore's computer setup, I've been envious.