“sag” / “sack” – a shortcut tool for ag / ack / grep:
(open sourced, on github: https://github.com/sampson-chen/sack)
Updated 2013-01-25: the_silver_searcher: Added support to use
ag as the search tool via
sag. (Clean-up and sgrep will have to wait a bit)
While learning and working with project codebases on the command line, I find myself constantly relying on
grep to navigate my way around. I soon realized that I spent entirely too much time on:
- Typing out the file paths from the
- Typing out the same flags for
grepevery time, and
- Moving between directories to initiate a search from the codebase root, and then having to move back to the directory (pressing tab a number of times) that I was originally working in:
Mental context-switches are expensive. Often, after redirecting attention to type out the long file paths (at times making silly mistakes with
cd, as above) and open the required files, you have already lost your train of thought. The time cost is now greater than just the 10-15 seconds from keying out a long file path.
This issue led me to thinking: “Gee, it sure would be nice to have a tool to handle these menial tasks automatically.” The search for such a tool on Google (and among fellow developers) yielded no results. So I decided to just write a tool for it: can’t possibly take that long, right?
I originally started with
sgrep, as in “s(hortcut)grep”. However, upon discovering ack (http://betterthangrep.com/), the replacement for grep, I decided to write sack – “s(hortcut)ack” instead.
Here is how sack can save you valuable time and mental effort:
We’ll use a similar example to the one before, but using sack instead:
Notice that it uses preset flags (“
-i“) for the current profile (“
RB“) and performs the search under a set of pre-specified directories.
(If you prefer not to use any preset flags / search directories, there is a empty profile called “
no_profile” for using sack exactly the same way you would with ack / grep.)
Now for the fun part! See the little tags that prefix the search results? Sack adds those to the search result of ack: instead of typing out a long file path to access the 59th result, simply do:
user@linux:~$ F 59
on the prompt and it will open the correct file with Vim (or Emacs, or another editor), and then even go directly to the line corresponding to the search result for you:
Note that whenever you perform a search in any terminal with sack, you can use the shortcuts in all other terminals (including new ones). So now you can use one terminal to keep the search results open for reference, and use other terminals to open the files of interest, via shortcuts.
Using profiles is easy. For example, here’s switching profiles in sack:
There are a number of other things you can do with profiles, such as renaming a profile, setting preset flags / search directories for a profile, adding new profiles – these are documented on the github page for sack, and
sack --help on the command line.
I shall leave the rest for you to explore. My hope is that this will make your life easier when navigating around codebases. If you have any suggestions on how you would improve this tool, please don’t hesitate to let me know! =)