Github Twitter 2 Delicious Linkedin Stackoverflow Google plus new 32px Rss

Switching to Sublime Text 2 Updated

Since I wrote my original post on switching to Sublime Text 2 a lot has changed. The final version was released just a few weeks ago. I also got a new computer and had to reinstall it; what I had originally written is a bit out of date so here is an updated post.

Command line Tool

To be able to open a file or folder from the command line from anywhere create a symlink to the subl command line tool.
$ sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/bin/subl

RVM

Edit Sublime Text's ruby settings file:
$ subl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/Ruby/Ruby.sublime-build

It should look like this:
{
  "cmd": ["ruby", "$file"],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.ruby"
}

Change it to this (replacing nickd with your username) in order to use the rvm-auto-ruby binary:
{
  "cmd": ["/Users/nickd/.rvm/bin/rvm-auto-ruby", "$file"],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.ruby"
}

Preferences File

Sublime Text 2 stores all settings and preferences in editable files with key/value pairs. If you want to change any default settings edit the User Preferences settings file and place your changes there. This will prevent them from being overridden in an upgrade.

Edit the User Preferences file:
$ subl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/Preferences.sublime-settings

You can also edit this file from within Sublime Text. Click the menu item: Sublime Text 2 => Preferences => Settings – User. I changed mine to use spaces instead of tabs and adjust the indentation globally. I also turned off hot exit, word wrap, and a few other things. If you just look at the default settings file you can get an idea of what's available to change. Just copy the keys to your own user file and change the values. I recommend checking this file into git as well.

{
  "tab_size": 2,
  "translate_tabs_to_spaces": true,
  "word_wrap": "auto",
  "hot_exit": false
}

To open all packages to see what's in them:
$ subl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/

Sublime Package Control

Sublime Package Control is a great addition to Sublime's ecosystem. There are ton of useful packages and themes that you can add just by selecting them. Just follow the simple installation instructions and you'll be good to go.
Press cmd + shift + p and type "Package Control" to find all the options for this add on. You can also get to it under the Preferences Menu.

The packages you have installed are listed in another settings file. Any changes you make to this file will be automatically picked up by the package manager. It is easy to just add or remove packages here as well.
$ subl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/Package\ Control.sublime-settings

Some useful ones I've installed are: CoffeeScript, Dogs Colour Scheme, Haml, RSpec, RubyTest, Sass, and SCSS. It's also a good idea to check this file into git.

Sublime Text 2 Ruby Tests Bundler Integration

When you try running tests from within Sublime Text it will probably error out if you are using Bundler. To fix this you need to override the command to execute the tests. Open you User Preferences file again and add:
{
  // RubyTest settings
  "ruby_unit_exec": "bundle exec ruby",
  "ruby_rspec_exec": "bundle exec rspec"
}

Sublime Text 2 Ruby Tests ANSI Color Fix

When running RSpec test from within Sublime Text 2 you'll notice that the output contains ANSI color codes.

To fix this you can edit this file:
$ subl ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/Theme\ -\ Default/Widget.sublime-settings
and comment out the color_scheme line and add the one listed below. This doesn't break any highlighting and will cause the ANSI color codes to be interpreted correctly.
{
  //"color_scheme": "Packages/Theme - Default/Widgets.stTheme"
  "color_scheme": "Packages/RubyTest/TestConsole.tmTheme"
}
This fix was posted on the Github Issues page

I hope this helps you out. If you have any suggestions or tips please post a comment!

Related Links

Comments

Rasmus
Rasmus says:
09/20/2012 04:06am

Also just made the switch (from TextMate). Excellent blog post to get starten on ST2. Thank you for taking the time to write it up :-)

Bill Gathen
Bill Gathen says:
09/20/2012 01:12pm

Even applying the RVM and Bundler edits above, Sublime still isn't picking up my project's .rvmrc file (it uses the default RVM, which doesn't happen to have rspec), which means RubyTest only works if I do the following:

  • cd to my project dir, so my shell picks up the project .rvmrc
  • Open Sublime using the subl command
  • Open the project's sublime-project file.

I think it's because I'm using zsh and the launchers aren't picking it up. Opening Sublime from the Dock or Spotlight sets my path to /usr/bin:/bin:/usr/sbin:/sbin. Not sure what to do to fix that part.

Quentin
Quentin says:
09/24/2012 07:42am

@Bill, to fix your problem, open ~/.bash_profile file and make sure it has the following.

export PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:~/bin:~/bin/subl

Nick DeSteffen
09/24/2012 09:30pm

@bill I think you are correct, I guess I never noticed that Sublime isn't automatically picking up the .rvmrc file. My workflow is just as you describe, cd to a project folder and launch using subl . I have noticed that when having multiple projects open the ruby-tests plugin only works with the first one I opened.

You could try @quentin's suggestion, however You'll need to edit your ~/.zsh_profile instead, since you are using zsh.

Bill Gathen
Bill Gathen says:
09/27/2012 10:44am

Thanks for the suggestions! I've continued to fiddle with my setup, but I think it's something about how Spotlight fires apps, regardless of the shell. Docs conflict, especially on Mountain Lion.

Just discovered the --project arg, so I can skip the "open project" step above and just do

subl --project <projectname>-sublime.project

I'll probably hack together a shell script that will do something like "if a filename was supplied as an arg, open it, otherwise look for a *-sublime.project file in the current dir and open that with the --project arg" and override subl with it. I'll post it here when I have a chance to work on it.

Tony Clark
Tony Clark says:
10/07/2012 07:53pm

Nick - can't thank you enough for the info. I was following your method and came across Sublime Text's (http://www.sublimetext.com/docs/2/osx_command_line.html) and Michael Hartl's (https://github.com/mhartl/rails_tutorial_sublime_text) as well.

QUESTION: Why do they tell use to do this: after creating the symlink:

$ ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

AND...why don't we do it in your thread?....or do we...

Many Thanks

Bill Gathen
Bill Gathen says:
10/08/2012 06:21am

After finding Brandon Keeper's post on this topic, this is what I ended up settling on, which gives me everything I need for daily development. No more messing with project files.

Press e to open the current dir in Sublime (my usual), or e <filepath> to open a file.

I added the following to my .zshrc file (put it in .bashrc if you're using bash):

I also edited my User/Preferences.sublime-settings to include the following:

This causes Sublime to forget what files you had open, for a more standard “open what I say to open” editor behavior.

Nick DeSteffen
10/08/2012 11:18am

@tony -- Glad I could help out. The first thing I suggest doing is link the subl command under the section Command line Tool. This step creates a symlink in your path, so that you can run the subl command from anywhere within your system. This is how I open most projects up.

David Berry
12/22/2012 05:12pm

This is excellent, worked first time. I was struggling to get the latest version of Ruby with Tk working. Now it all works great.

I hope they make this easier from Sublime Text sometime soon!

Format using Markdown