Setting up Sublime Text for Ruby development

Sublime Text 3 (ST3) is a lightweight, cross platform, blazing fast text editor with Chrome-like tabs and split window layouts. There is a relatively huge amount of support for it, because there are many who have fallen in love with it. At last count there were over 1,300 “packages” available for it to enhance its functionality, appearance, and general usability. Once you have a few settings tweaked and packages installed, which is a very simple process, you’ll really come to like Sublime Text. And you will need to set up some preferences, because out of the box it’s pretty standard. It’s so easy, I’ll just tell you quickly how to do that now.

Why Sublime Text?

After switching from PHP to Ruby half a year ago I was looking for a new IDE to replace NetBeans. I’ve tried RubyMine and realised that PHP’s verbosity is why I went for NetBeans and I no longer need a monstrous IDE with Ruby, a plain text editor will be fine. I used Ubuntu at the time and Sublime Text 2 was the best option. Having tried TextMate on my MacBook, I went back to Sublime Text. You should switch to it, especially if you’re using TextMate.

Getting Started

Here are the steps I usually follow to take a new ST3 install from good to awesome:

  1. Install the subl command line tool. Assuming ~/bin is in your path:
    ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl

    On Ubuntu, I recommend installing ST3 from PPA, it will spare you from symlinking.

    Optionally, you can now make Sublime Text the default editor for git:
    git config --global core.editor "subl --new-window --wait"

  2. Install Package Control. Package Control makes it easy to install/remove/upgrade ST3 packages.

    Follow these instructions and reboot ST3 when done. (You only need to do this once. From now on you’ll use Package Control to manage ST3 packages.)

  3. Install the Soda theme and RailsCasts color scheme. This dramatically improves the look and feel of the editor.

    Use Package Control to install Soda:

    • Press ⌘⇧P to open the Command Palette.
    • Select “Package Control: Install Package” and hit Enter.
    • Type “Theme - Soda” and hit Enter to install the Soda package.

    Do the same for the package called “RailsCasts Colour Scheme”.

  4. Start with a basic Settings file. You can use mine, which will activate the Soda Light theme and RailsCasts color scheme. Reboot Sublime Text 3 so the Soda theme applies correctly. You can browse the default settings that ship with ST3 by choosing Sublime Text > Preferences > Settings - Default to learn what you can configure.

  5. Install more packages. My essentials are All Autocomplete, BeautifyRuby, ChangeQuotes, CoffeeScript, SideBarEnhancements and GitGutter. GitGutter shows whether each line has been added, modified, and where lines have been removed.

Leveling Up with Custom Commands

Sublime Text makes it dead simple to write your own commands and key bindings in Python. Here are some custom bindings that I use:

  • Copy the path of the current file to the clipboard. Source
  • Close all tabs except the current one. Source
  • Convert Ruby 1.8 hashes to Ruby 1.9 syntax. Source

New key bindings are installed by dropping Python class files in ~/Library/Application Support/Sublime Text 3/Packages/User and then binding them from Default (OSX).sublime-keymap in the same directory. You can use mine as an example.

Getting your hands on a keyboard shortcut cheat sheet is recommended, although you can of course set your own key bindings as you want them. Knowing keyboard shortcuts separates the men from the boys, and increases your productivity exponentially.

Code Navigation

Sublime Text 3 introduces symbol indexing, which works by scanning all files in the project folder. The new Goto > Goto Definition and Goto > Goto Symbol in Project options let you quickly navigate to code definitons (classes, methods, etc.). You could get the same functionality in ST2 by installing CTags.

There is one small change needed for it to work nicely with Ruby’s bang! and predicate? methods. Put this in your settings file at Sublime Text > Preferences > Settings - User:

"word_separators": "./\\()\"'-:,.;<>~@#$%^&*|+=[]{}`~"

This is the default setting without exclamation and question marks and is needed for ST3 to treat them as part of the word.

Conclusion

As you can see, Sublime Text is not a fully featured editor from the beginning. But exten­si­bil­ity is one of its great­est fea­tures, among many great ones. I hope this setup guide was helpful to you. If you’ve got any comments or suggested improvements, let me know in the comments below or drop me a line on Twitter.