Development

Searching for Silver Bullets

Searching across a project’s codebase for keywords is a pretty standardrequirement when starting something new. Recently I inherited a very large Rails app with 238 models and 348 controllers. It was pretty clear that I was going to have to find a way to explore certain parts of the code by looking for what I was working on that day in isolation.

2 min
August 19, 2016
Jon Kinney
Partner & CTO

Searching across a project's codebase for keywords is a pretty standard
requirement when starting something new. Recently I inherited a very large
Rails app with 238 models and 348 controllers. That is to say nothing of
the number of view and JavaScript files as well as service objects, presenters,
etc. It was pretty clear that I was going to have to find a way to explore
certain parts of the code by looking for what I was working on that day in
isolation.

Lots of editors have this capability built in including: RubyMine, Sublime
Text, Atom, and Textmate, but at Headway we use console Vim inside of Tmux for
the majority of our development, and project-wide search isn't provided out of
the box. This led me to try several different "grep like" tools over the years,
including just plain [grep](https://en.wikipedia.org/wiki/Grep). However, with
thousands of files to search through, I needed speed. For many years I was
happily served by a tool called [Ack](http://beyondgrep.com/documentation/),
which bills itself as a much faster grep replacement. But even with all the
improvements over vanilla grep that Ack made, it was still too slow for my
needs. Enter [The Silver
Searcher](https://github.com/ggreer/the_silver_searcher), or "Ag" as it's
known.

Ag starts out claiming that "it is an order of magnitude faster than ack".
A bold statement to be sure, but I can say with certainty that it is
indeed worth the upgrade.

## Installing Ag

To install Ag, use [homebrew](http://brew.sh/): `brew install
the_silver_searcher`. For other platforms, see the
[readme](https://github.com/ggreer/the_silver_searcher).


## Configuring Ag To Ignore Directories

Without any configuration, Ag will search through all the files in
a directory except for those matched by ignore patterns specified in
the project's `.gitignore` or `.hgignore` or `.agignore` files. You can
also create a `~/.agignore` file at the root of your home directory for
global use.

Ignoring some directories in a Rails app like `/public/system` and
`/coverage` that aren't typically found in the `.gitignore`, can have
a dramatic impact on the speed at which results are returned from Ag.
Without a tuned `.agignore`, in the project I mentioned earlier, Ag would
take 15 seconds to return results. With a tuned `.agignore` file though,
I was able to get results back in under a second! Ack or grep were
unusable and just locked up my machine.

The `.agignore` pattern is really straightforward. You simply list the
folders or files you want to ignore by name like so:

   -- CODE line-numbers language-lang --
   <!--
   /cache/
   /HTML_DEMOS/
   /log/
   /db/migrate/
   /public/demos/
   /public/system/
   /public/images/
   /public/html/
   /production_data/
   /cookbooks/
   /vim_sessions/
   /coverage/
   /tmp/
   /compiled/
   /gems/
   /.idea/
   *.idea
   tags
   .zenflow-log
   /app/assets/fonts
   /app/assets/html
   /app/assets/images
   -->

## Performing Advanced Searches

One of the tasks that I needed to perform involved deleting a whole workflow
from the app. This meant that while I wanted to delete code that included text
like "news" I didn't want to include text like "newsignword". In order to find
only items that stopped after the four characters "news" I needed to use
a negative lookahead regex.

Ag uses Perl-Compatible Regular Expression (PCRE) syntax, which I wasn't
overly familiar with, so here are a few sites that I used to help test my
regex:

 * [https://regex101.com](https://regex101.com)
 * [https://www.debuggex.com/cheatsheet/regex/pcre](https://www.debuggex.com/cheatsheet/regex/pcre)

I ended up with this:

   -- CODE line-numbers language-lang --
   <!--
   news(?!ignword|tyle)
   -->

This would search for the string "news" but omit results that would
include "ignword" or "tyle" immediately following "news". So while all the
"news" hits showed up, I was able to filter out tens of hits for
"newsignword" and "newStyle".


## Wrap up

Next time you need to get up to speed quickly on a new codebase, give Ag
a shot! Coupled with other tools like [ctags](http://ctags.sourceforge.net/),
and [rails.vim](https://github.com/tpope/vim-rails), [Vim](http://www.vim.org/)
is a great IDE for Ruby on Rails development! Also, check out the cool things
happening in the [neovim](https://neovim.io/) space!

Actionable UX audit kit

  • Guide with Checklist
  • UX Audit Template for Figma
  • UX Audit Report Template for Figma
  • Walkthrough Video
By filling out this form you agree to receive our super helpful design newsletter and announcements from the Headway design crew.

Create better products in just 10 minutes per week

Learn how to launch and grow products less chaos.

See what our crew shares inside our private slack channels to stay on top of industry trends.

By filling out this form you agree to receive a super helpful weekly newsletter and announcements from the Headway crew.