Get to Know a Gem: Rak
A few months ago, I posted about an article that showed you how to colorize your grep search results. Since then, I’ve heard people talking about ack, which describes itself as…
“a tool like grep, aimed at programmers with large trees of heterogeneous source code.”
It’s written in Perl, which is fine and dandy… but before I installed it, I heard that there was a Ruby version named rak, which describes itself as…
“a grep replacement in pure Ruby. It accepts Ruby syntax regular expressions and automatically recurses directories, skipping .svn/, .cvs/, pkg/ and more things you don’t care about. “
Sounds great. Let’s see what this thing can do.
Installing rak
Daniel Lucraft, the author of rak, was kind enough to package it up as a Rubygem. So, all we have to do is install it via gem install rak
.
> sudo gem install rak
Password:
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed rak-0.8.0
Installing ri documentation for rak-0.8.0...
Installing RDoc documentation for rak-0.8.0...
~ >
Great, let’s move on.
Using rak
Now that it’s installed, we can use Rak by typing rak
from the command line. You’d typically want to run this from within the root of your application.
For example, basic usage would look like the following.
$ rak search-pattern
In my first test, I ran rak README
.
Immediately, I see a greater advantage to rak
over using grep
and that’s because it’s giving me line numbers for free, which takes remembering a few extra options with grep.
Like grep
, we can specify a specific path to search with. For example, we use a view helper named link_to_unimplemented
to help us track actions that aren’t implemented yet. Looking at a current project, I can run rak link_to_unimplemented app/views
and produce the following results.
I’m going to keep playing with it, but wanted to help get the word out. If you have any tips on using it, please share them in the comments. :-)
Spice up your Terminal with colored grep pattern results
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 !
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. :-)