Using temporary shell aliases

Posted on: December 6, 2012

Many of us have shell aliases defined in our configuration files. But we may forget that we can set up quick, temporary ones for whatever task we’re doing.

For example, I’ve been improving the readability of Authority’s spec output. While I’m doing this, I want to see verbose output. I also want see my output in a consistent order, so I don’t want to run my tests in a random order like I normally would.

So I run a command like this, and check the output:

$ rspec spec/authority/controller_spec.rb --format doc --order default

Editing that to run a different spec is a bit annoying:

$ rspec spec/authority/abilities_spec.rb --format doc --order default
                     ^
                     change here

But I can make this simple. First, rearrange the command a bit so that the path is at the end:

$ rspec --format doc --order default spec/authority/abilities_spec.rb

Then, set a quick alias for running with these specific options:

$ alias docspec='rspec --format doc --order default'

Now I just use that on whatever file or folder I want:

$ docspec spec/authority/abilities_spec.rb
$ docspec spec

My shell will forget the alias when I exit, and that’s fine; I only need it right now.

If I find myself doing this a lot, maybe I’ll add it to my config file.