Error Log: 20DEC2015

Topics Include:


Aliases:  Use them, sport.

In our current project we’re using Minitest as our main testing platform and one of the most common ways we run individual tests is via the following command line command:

bundle exec ruby -I lib -I test -I app test/path/path_to_test -n test_name

As you can imagine, trying to write this every time you want to run a test would be tedious.  I know what you’re saying, “Well technically I only need to write it once and then I can just hit the “down” arrow key to have it cycle through the previously used commands and just change what I need to.”  Hell I said it and have been doing just that.

The problem with that method is that if you close that terminal window you lose that command history or if you have a lot of different tasks going on in that window (running tests, committing to Github, and so on) you can easily have to do some digging to find that command which you’ll potentially edit for a different file/test anyway.  When I started to count up the time I was spending trying to either type the command by hand or trying to find it in the list of previously used commands, I was wasting several seconds each time which over the course of a day/week/month really adds up to lost time and attention.

The solution:  Setup an alias!  Aliases are terminal/bash command-line shortcuts that enable you to use a short strings of characters to represent longer commands.  In this case I replaced that aforementioned line with rit which, for me, stands for “Ruby Individual Test”.  To be honest I borrowed the alias name from our Senior Developer, Jake Gordon, so it likely means something much different in reality but oh well.

To setup an alias you use the following format:

alias shortcut="command"

So in the case of “rit” it would look like this:

alias rit="bundle exec ruby -I lib -I test -I app"

So now when I want to run a test I just type in rit test/models/model_test.rb -n test_name.  While it may not seem like much time savings, it definitely helps out in the long haul as all those seconds start to add up over the life of a project.

One important thing to note, just setting the alias at the command line only saves it for that particular terminal session. If you close that terminal that alias disappears.  To get around this you’ll need to add the alias to a file that’s read when the terminal is launched like ~/.bashrc or ~/.bash_profile.  Using your favorite editor (Atom, Sublime, or even just nano) add that same alias rit="bundle exec ruby -I lib -I test -I app" line at the bottom of the file and then save.  Voila!  Now your alias will work every time!

The resource that helped me most (in additions to Jake’s prodding/mentoring) when trying to get this working was a post on Jonathan Suh’s blog, Terminal/Bash Command-Line Shortcuts with Aliases.

Tab Completion for Fame and Glory

Tab completion is something that ends up being for much the same purpose as using the Terminal/Bash aliases I mentioned above.  While the time savings per action are minimal, over the life of the project (and your career!) those few seconds really add up and can really speed up your overall workflow efficiency.

So what is tab completion?

Well in most modern text/code editors (Sublime, Atom, Vim, etc.) and in Terminal/Bash command-line prompts you can often type part of a command/path and then hit tab on your keyboard to auto-complete the rest of the item.  While for short commands and paths this isn’t that big of a time saver, but once you start having to traverse a deeply nested path with longer directory names and/or typing out lengthy variable names, you can quickly zip through them.

As an example for Bash/Terminal path completion, here’s a slightly modified path I had to access quite a bit for testing just a few weeks back:

rit test/plan/controllers/plan/location_controller.rb -n test_location

Now using tab completion I was able to shorten it to essentially this:

rit t/p/c/p/l -n test_location (hitting tab after each of the individual letters then the forward slash)

If you’re like me and get a little fat fingered with your typing, the fewer keystrokes you have to make means the less time going back to fix a spelling error in a path name/variable name that you didn’t immediately catch.

Leave a Reply

Your email address will not be published. Required fields are marked *