Read my latest article: Launching Rails projects, an open call for lessons learned (posted Tue, 23 Jun 2009 17:33:00 GMT)

Spice up your Terminal with colored grep pattern results

Posted by Robby Russell Sat, 06 Oct 2007 17:43:00 GMT

11 comments Latest by Dan Johnson Sun, 07 Oct 2007 21:38:51 GMT

Earlier, I came across a post by Garry Dolley, which he shows how to acheive colorized grep matches in bash. I recall having color matches when I used to use Linux on a daily basis as my primary work environment, but haven’t gotten around to setting this up on my MacBook, which is where I do almost all of my development work.

Before

If you don’t already have colors, a grep in your terminal might look something like the following screenshot.

While, I have a very small output here, this gets much crazier when you’re using egrep across an entire project. It’s hard to scan through all of the results for the inline pattern matches.

So, taking Garry’s suggestion (for bash), I did something similar with my favorite shell, Z shell.

Add the following to your ~/.zshrc file to begin experimenting with the colors.


  export GREP_OPTIONS='--color=auto' 
  export GREP_COLOR='1;36'

After

With the new variables defined in my .zshrc, I can now start to see colors showing up in my grep results.

Pretty cool, huh?

Variants

To save you the trouble of trying tons of combinations yourself, which I suspect you’ll do anyways, here are some other variants.

Blinking

If you change the first number in GREP_COLOR to 5, you’re matches will blink!

You’ll have to experiment with this yourself as I’m not going to make a video for you. ;-)


  export GREP_COLOR='5;35'

Inverted Colors

You can also invert the colors so that the background color changes on your pattern matches.

For example:

To achieve this, you can set the first number in GREP_COLOR to 7.

...and so much more

I decided to write a quick and ugly ruby script to iterate through the color combinations that I was trying.

Anyhow, I’ll leave you on that note. If you figure out how to do any other fun things with grep colors, do let me know. :-)

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Josh Sat, 06 Oct 2007 19:17:35 GMT

    Wow, this is slick! Thanks!

  2. Avatar
    Reagan Sat, 06 Oct 2007 19:20:20 GMT

    My grep is now coloured with 7;33. Thanks for posting this. :-)

  3. Avatar
    sapporo Sat, 06 Oct 2007 19:58:19 GMT

    The nice thing about --color=auto (in contrast to --color=always) is that it won’t colorize anything unless the output goes to a terminal. Therefore you can safely pipe output to, say, /usr/bin/less, without seeing the color control characters.

  4. Avatar
    heavysixer Sat, 06 Oct 2007 20:32:18 GMT

    Yeah Garry knows some dope rails magic, he also has some good postings on git.

  5. Avatar
    Garry Sat, 06 Oct 2007 20:53:17 GMT

    Nice screenshots! I never knew about the blink code either ;)

  6. Avatar
    Robby Russell Sat, 06 Oct 2007 21:54:41 GMT Recommend me on Working with Rails

    Yeah, the blinking one is cool, but it can be overwhelming when you get a lot results. ;-)

  7. Avatar
    pmccann Sun, 07 Oct 2007 02:31:13 GMT

    Just to throw a curveball into this lovefest: I highly recommend you have a look at “ack”, Andy Lester’s perl based grep replacement. Why would you want to do that? Well, it works beautifully out of the box, ignores svn/cvs/backup files out of the box, is already configured to search, say, just ruby files in your source tree, etc etc.

    So a simple

    ack --ruby '\d{2}'

    shows all occurrences of “double digits” in .rhtml,.rb,.rjs files beneath your current directory, grouped by file, with coloured output.

    See

    use.perl.org

    if you’re interested. Yes, it’s perl, but you can forget that once it’s installed (if you must…).

  8. Avatar
    James O'Kelly Sun, 07 Oct 2007 09:10:01 GMT

    My gawd! That is the first time I have seen a blink tag an a loooooong time :) Cool grep coloring tute!

  9. Avatar
    AnĂ­bal Rojas Sun, 07 Oct 2007 14:05:33 GMT

    Ha ha ha, I considered myself a shell junkie, but the idea of having a blinking match made feel a old shell junkie with a tocuh of HTML 1.0

  10. Avatar
    Dan Johnson Sun, 07 Oct 2007 21:38:38 GMT

    You can make the colorizing work with less too. With the—color=auto grep won’t output the colorizing control codes unless it is outputing to a terminal. Similarly “less” won’t render them.

    grep -Ri—color=always ruby rubygems-0.9.0 | less -R

    gave me gobs of red highlighted lines to scroll through.

  11. Avatar
    Dan Johnson Sun, 07 Oct 2007 21:38:51 GMT

    You can make the colorizing work with less too. With the—color=auto grep won’t output the colorizing control codes unless it is outputing to a terminal. Similarly “less” won’t render them.

    grep -Ri—color=always ruby rubygems-0.9.0 | less -R

    gave me gobs of red highlighted lines to scroll through.

Share your thoughts... (really...I want to hear them)

Comments