Robby on Rails: Spice up your Terminal with colored grep pattern resultsthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2007-10-06T12:48:44-04:00Robby Russellurn:uuid:0bc194d2-edc7-4df5-ad4a-298061ef9d9f2007-10-06T12:43:00-04:002007-10-06T12:48:44-04:00Spice up your Terminal with colored grep pattern results<p>Earlier, I came across a post by Garry Dolley, which he shows how to acheive <a href="http://scie.nti.st/2007/7/18/colorized-grep-matches">colorized grep matches</a> 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.</p>
<h2>Before</h2>
<p>If you don’t already have colors, a grep in your terminal might look something like the following screenshot.</p>
<p><img src="http://myskitch.com/robbyrussell/terminal_sans_colors-20071006-104657.jpg" alt="" /></p>
<p>While, I have a very small output here, this gets much crazier when you’re using <code>egrep</code> across an entire project. It’s hard to scan through all of the results for the inline pattern matches.</p>
<p>So, taking Garry’s suggestion (for <code>bash</code>), I did something similar with my favorite shell, <a href="http://en.wikipedia.org/wiki/Zsh">Z shell</a>.</p>
<p>Add the following to your <code>~/.zshrc</code> file to begin experimenting with the colors.</p>
<pre><code>
export GREP_OPTIONS='--color=auto'
export GREP_COLOR='1;36'
</code></pre>
<h2>After</h2>
<p>With the new variables defined in my <code>.zshrc</code>, I can now start to see colors showing up in my grep results.</p>
<p><img src="http://myskitch.com/robbyrussell/grep_with_colors-1-20071006-104732.jpg" alt="" /></p>
<p>Pretty cool, huh?</p>
<h2>Variants</h2>
<p>To save you the trouble of trying tons of combinations yourself, which I suspect you’ll do anyways, here are some other variants.</p>
<h3>Blinking</h3>
<p>If you change the first number in <code>GREP_COLOR</code> to <strong>5</strong>, you’re matches will <blink><strong>blink</strong></blink>!</p>
<p>You’ll have to experiment with this yourself as I’m not going to make a video for you. ;-)</p>
<pre><code>
export GREP_COLOR='5;35'
</code></pre>
<h3>Inverted Colors</h3>
<p>You can also invert the colors so that the background color changes on your pattern matches.</p>
<p>For example:</p>
<p><img src="http://myskitch.com/robbyrussell/default-3-20071006-104539.jpg" alt="" /></p>
<p>To achieve this, you can set the first number in <code>GREP_COLOR</code> to <strong>7</strong>.</p>
<h3>...and so much more</h3>
<p>I decided to write a quick and ugly ruby script to iterate through the color combinations that I was trying.</p>
<p><img src="http://myskitch.com/robbyrussell/coloring_egrep-20071006-104018.jpg" alt="" /></p>
<p>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. :-)</p><p>Earlier, I came across a post by Garry Dolley, which he shows how to acheive <a href="http://scie.nti.st/2007/7/18/colorized-grep-matches">colorized grep matches</a> 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.</p>
<h2>Before</h2>
<p>If you don’t already have colors, a grep in your terminal might look something like the following screenshot.</p>
<p><img src="http://myskitch.com/robbyrussell/terminal_sans_colors-20071006-104657.jpg" alt="" /></p>
<p>While, I have a very small output here, this gets much crazier when you’re using <code>egrep</code> across an entire project. It’s hard to scan through all of the results for the inline pattern matches.</p>
<p>So, taking Garry’s suggestion (for <code>bash</code>), I did something similar with my favorite shell, <a href="http://en.wikipedia.org/wiki/Zsh">Z shell</a>.</p>
<p>Add the following to your <code>~/.zshrc</code> file to begin experimenting with the colors.</p>
<pre><code>
export GREP_OPTIONS='--color=auto'
export GREP_COLOR='1;36'
</code></pre>
<h2>After</h2>
<p>With the new variables defined in my <code>.zshrc</code>, I can now start to see colors showing up in my grep results.</p>
<p><img src="http://myskitch.com/robbyrussell/grep_with_colors-1-20071006-104732.jpg" alt="" /></p>
<p>Pretty cool, huh?</p>
<h2>Variants</h2>
<p>To save you the trouble of trying tons of combinations yourself, which I suspect you’ll do anyways, here are some other variants.</p>
<h3>Blinking</h3>
<p>If you change the first number in <code>GREP_COLOR</code> to <strong>5</strong>, you’re matches will <blink><strong>blink</strong></blink>!</p>
<p>You’ll have to experiment with this yourself as I’m not going to make a video for you. ;-)</p>
<pre><code>
export GREP_COLOR='5;35'
</code></pre>
<h3>Inverted Colors</h3>
<p>You can also invert the colors so that the background color changes on your pattern matches.</p>
<p>For example:</p>
<p><img src="http://myskitch.com/robbyrussell/default-3-20071006-104539.jpg" alt="" /></p>
<p>To achieve this, you can set the first number in <code>GREP_COLOR</code> to <strong>7</strong>.</p>
<h3>...and so much more</h3>
<p>I decided to write a quick and ugly ruby script to iterate through the color combinations that I was trying.</p>
<p><img src="http://myskitch.com/robbyrussell/coloring_egrep-20071006-104018.jpg" alt="" /></p>
<p>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. :-)</p>