Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Last active December 10, 2023 20:00
Show Gist options
  • Save pmarreck/2367908 to your computer and use it in GitHub Desktop.
Save pmarreck/2367908 to your computer and use it in GitHub Desktop.
Get Sublime Text 2 (or 3) to use your RVM ruby and bundle Gemfile
# Get Sublime to use your rvm ruby... Change your ~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build
# (for ST3: ~/Library/Application Support/Sublime Text 3/Packages/User/Ruby.sublime-build) to this, replacing YOURUSERNAME.
# I am still looking to optimize this further... For example, avoiding hardcoding by using something like $HOME, although
# I have yet to get any form of that to work.
{
"working_dir": "${project_path}",
"cmd": [
"/Users/YOURUSERNAME/.rvm/bin/rvm-auto-ruby", "-Ilib:test", "$file"
],
// "path": "/Users/YOURUSERNAME/.rvm/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin",
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby"
}
@klebervirgilio
Copy link

For some reason I had to replace $HOME for /Users/username on Mac.

@Hengjie
Copy link

Hengjie commented Dec 6, 2012

This didn't work for me as it was looking for bundle in /bin, /usr/bin, /usr/sbin, /sbin, despite environment variables for $PATH being set correctly. I decided to look else where and found this that works: http://rubenlaguna.com/wp/2012/02/01/sublime-text-2-rvm-rspec/

@kitplummer
Copy link

For Sublime Text 3 I just dropped it in ~/Library/Application/Support/Sublime Text 3/Packages/User/ - works great!

@fny
Copy link

fny commented Jul 18, 2013

You're missing a comma

 "cmd": [
    "bundle", "exec", "$HOME/.rvm/bin/rvm-auto-ruby", "$file"
  ], # <-- here

@pmarreck
Copy link
Author

Thanks for the tips!

@ilium007
Copy link

I get "Could not locate Gem file"

I found this on another site and it worked fine:

{
"cmd": [ "/Users/xxxxxxx/.rvm/bin/rvm-auto-ruby", "$file" ],
"file_regex": "^(...?):([0-9]):?([0-9]*)",
"selector": "source.ruby"
}

@shredding
Copy link

For sublime linter, you have to add

{ 
    "sublimelinter_executable_map":  {
            "ruby": "/Users/YOURUSERNAME/.rvm/bin/ruby"
    }
}

... to the sublime linter settings as well.

@pmarreck
Copy link
Author

I tweaked the original gist, it now works on my Sublime Text 3 editor

@fny
Copy link

fny commented Dec 17, 2013

All you need is "env": { "PATH": "${HOME}/.rvm/bin:${PATH}" } to dodge the whole USERNAME nonsense. See my fork for the full details.

@tgmerritt
Copy link

For whatever reason - I did not have /Users/MYUSER/.rvm

rvm installed itself into /usr/local/rvm

So my solution was to create a symbolic link:

$ ln -s /usr/local/rvm/ .rvm

From my $HOME dir

I also used the code at the very top of this gist, but if you still have issues when starting ST3 where it says it's using a version of Ruby other than what you think it should use, check to ensure that .rvm exists where people are saying it should exist.

I did nothing custom on my rvm installation - just followed the guide and left everything at default.

@v66moroz
Copy link

Slightly more sophisticated example for ST2 for Rails developers (Ctrl+Shift+B will call rails runner):

{
        "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
        "selector": "source.ruby",
        "shell": true,
        "cmd": ["rvm . do bundle exec ruby $file"],
        "variants": [
                {
                        "name": "Run",
                        "cmd": ["rvm . do bundle exec rails runner $file"]
                }
        ]
}

@billforward-alex
Copy link

Fixed the $HOME problem:

{
  "working_dir": "${project_path}",
  "cmd": [
    "rvm-auto-ruby", "$file"
  ],
  "path": "${HOME}/.rvm/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin",
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.ruby"
}

@curtds
Copy link

curtds commented Jan 17, 2016

Where are the gemfiles in Text Sublime 2?

I am just getting ready to learn Web Developing with the Odin Project. At this time I am installing everything and I am at the Title: "Prepare your rails app for deploying to Heroku." It says to "Launch your text editor and open the "Gemfile" file located inside of your test_app folder." I am using Text Sublime 2 but I am unable to find the Gemfiles or even the test_app. Thanks for your help.

@smanolloff
Copy link

smanolloff commented Mar 11, 2017

Using rvm-auto-ruby is inefficient as it initializes the RVM environment on each invocation and has a noticeable performance impact.

Here is my solution for Sublime Text 3 on MacOS (commands require superuser privileges):

  1. Create file /Applications/Sublime Text.app/Contents/MacOS/setenv.sh with the contents of this gist

  2. chmod +x "/Applications/Sublime Text.app/Contents/MacOS/setenv.sh"

  3. Edit /Applications/Sublime Text.app/Contents/Info.plist, changing the CFBundleExecutable value to setenv.sh

  4. Force update the LaunchService database in the Terminal by using the lsregister command:

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f '/Applications/Sublime Text.app'
  5. (Re-)launch Sublime Text. Observe how the correct ruby (as set by rvm) invocations are almost instant within sublime :)

Bonus hint:

Invocations of various ruby executables (bundle, rspec, etc.) are much faster when done via ruby -S <executable> (at least for me on MacOS 10.12 with updated rubygems and rvm). Haven't really investigated why, but it sometimes saves up to one second...

Try it yourself:

command time ruby -S bundle exec rake --help >/dev/null
#        1.06 real         0.59 user         0.09 sys
command time bundle exec rake --help >/dev/null
#        2.14 real         1.15 user         0.09 sys

Hope this helps.

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