UCOSP Blog Post – sack and other developer shortcuts

Introduction:

Hello! My name is Sampson Chen. I just finished 3rd year Computer Science at University of Waterloo and I am currently on a Fall co-op term with Research In Motion.

Coming from a mostly Windows background (and working solely with IDEs for school work), participating in UCOSP + ReviewBoard so far is providing me with invaluable exposure to industry tools and practices such as Linux, bash, vim, git, Python, Django, and code-reviewed / test-driven development. The amount of material to learn may seem overwhelming, but it is also exhilarating to feel so empowered from learning how to use new tools. I find it especially exciting to contribute to a project whose purpose & vision you can believe in.

“sack” – a shortcut tool for ack / grep:

While I was learning and working with the Review Board codebase (reviewboard + rbtools + djblets) in Linux and the bash shell, I find myself having to rely on the grep tool constantly to help navigate my way through the code. It was not long before I realized that I spent a lot of time on:
1) typing out the file paths from the grep search results,
2) typing out the same flags for grep every time, and
3) 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:


(“arggghh!!!!!”)

Mental context-switches are expensive. Often, by the time I refocus my attention to actually typing out the long file paths (at times making silly mistakes with cd like in the above screencap) and open the needed files, I have already lost my train of thought. The time cost is now greater than just the 30 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 for me”. The search for such a tool on Google (and among CS friends better versed in Linux) yielded no results. So I decided to just write a short script 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.

(sack on github: https://github.com/sampson-chen/sack)

Here is how sack can save you valuable time and mental effort:

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, 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!

Huzzah, productivity!!!

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 a large codebase such as ReviewBoard’s. If you have any suggestions on how you would improve this tool, please don’t hesitate to let me know! =)

P.S. Other shortcuts for common dev tasks on ReviewBoard: http://reviewboardstudents.wordpress.com/2012/09/23/convenience-scripts-shortcuts-for-rb-development/ (updated)

P.P.S. It was pointed out to me that the install one-liner for sack on github was broken. This has now been fixed.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s