Skip to content

Instantly share code, notes, and snippets.

@bih
Last active December 28, 2015 11:19
Show Gist options
  • Save bih/7493092 to your computer and use it in GitHub Desktop.
Save bih/7493092 to your computer and use it in GitHub Desktop.
function r() {
# Use dot notation for file structure
# "app.assets.stylesheets" == "app/assets/stylesheets"
REPLACE="/"
INPUT="."$1
INPUT=${INPUT//./$REPLACE}
# No longer need to prepend with /
REPLACE="/"
INPUT=${INPUT//\//$REPLACE}
# Is there no parameter. Do nothing then.
if [ -z $1 ]; then
# # echo "HI WORLD!"
return
# Relative folder? Go with that.
elif [ -d ./$1 ]; then
cd ./$1
# Does a absolute folder exist? Go with that.
elif [ -d $(git rev-parse --show-cdup)"$INPUT" ]; then
cd $(git rev-parse --show-cdup)"$INPUT"
# Does a relative file exist? Go with that.
elif [ -e ./$INPUT ]; then
subl ./$INPUT
# Does a absolute file exist? Go with that.
elif [ -e $(git rev-parse --show-cdup)"$INPUT" ]; then
subl $(git rev-parse --show-cdup)"$INPUT"
# Is the r comamnd empty.
else
return
fi
}

The old "r" command to make Rails easier to use.

An improved version released as bih/rails-r has been released. Check it out.

I really don't like navigating through folders when building a Rails app. Folders are cool though. Anyway, I built a cool command that I use which allows you to swiftly move across Rails folders. I call it the r command.

Here is some examples. You can be in any subfolder of a Rails project and the command will work.

  • Open up the Gemfile for the current Rails project I am on. r Gemfile

  • Go to the stylesheets You can even put an alias on the below (I have). r app/assets/stylesheets

You'll need Git to use this. Works with non-Rails projects too (Git helps figure root directory + rest is pure bash).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment