Sunday, May 19, 2013

Bash aliases and variables don't mix

For a long time, I've used bash aliases such as:
alias hgrep='history | grep $1'
and they've worked just fine. However, when I've tried to do something a bit more complicated, such as:
alias hprju='heroku $1 --app project $2'
it fails with an error message:
!    `--app` is not a heroku command.
Bummer! What I finally realized is that the first example isn't actually replacing $1 with what I specified on the command line. Instead, $1 has NO replacement and my additional text is simply appended to the end of the line. To see how this works, just run the following:
alias test='ls'
test /etc
and you'll get a directory listing of /etc. After some googling -- with a number of questions along the lines of "Why doesn't my first example work and not my second?" -- I found this link, where the 2nd answer had the key. The Bash Reference Manual section on Aliases clearly states:
There is no mechanism [my emphasis] for using arguments in the replacement text, as in csh. If arguments are needed, a shell function should be used (see Shell Functions).
This situation has bugged me off and on for some time and I'm glad to finally understand what's happening. Hope it helps someone else.

Tuesday, April 23, 2013

RVM and vim-rails on Linux

I recently installed Tim Pope's vim-rails plugin (amazing stuff) but pretty quickly ran into a problem running things like the :Rake or :Rscript commands.  I'd get something like the following:

Could not find ZenTest-4.9.1 in any of the sources 

Checking things from the command line showed that ZenTest was, in fact, properly installed.  Finally, the output from a failed :Rake command gave me the clue I needed to resolve this issue:

/home/jseidel/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.1.5/lib/bundler/spec_set.rb
in `block in materialize':
Could not find gherkin-2.12.0 in any of the sources (Bundler::GemNotFound)
The ruby version vim-rails was using was the default
"/ruby-1.9.3-p194@global"
instead of the project-specific ruby I had specified of
"/ruby-1.9.3-p327@Rails3212"

The problem was that I was starting Vim from the system application launcher which is where it picked up the default values for $GEM_HOME since no project was associated.

Simple work-around is to start vim from the project command line and now all the environment settings are correct.

EDIT-1: Just found vim-rvm, again by Tim Pope, which takes care of all of this and then some.

EDIT-2: To get things to work together properly (I'm on RVM 1.19.6 stable and the latest vim-rvm and rim-rails), you have to convert your .rvmrc file to a .ruby-version file using the command:

rvm rvmrc to [.]ruby-version