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.RVM
Edit Sublime Text's ruby settings file: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:
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:
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.
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:
//"color_scheme": "Packages/Theme - Default/Widgets.stTheme"
"color_scheme": "Packages/RubyTest/TestConsole.tmTheme"
}
I hope this helps you out. If you have any suggestions or tips please post a comment!
Comments
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
sublcommand - 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.
@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
@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.
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.
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
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):
function e {
subl # need to open the app first: no clue why
subl -n ${1:-"."}
}
I also edited my User/Preferences.sublime-settings to include the following:
{
"hot_exit": false,
"remember_open_files": false
}
This causes Sublime to forget what files you had open, for a more standard “open what I say to open” editor behavior.
@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.
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!
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 :-)