Make your Rails text editing Sublime

Adam Barcan
In the weeds
Published in
4 min readOct 14, 2016

Put five programmers in a room and you will get five opinions (at least) on the superior text editor or IDE. The Vim-Emacs debate alone is the stuff of cliche.

I’ll state up front that I like Sublime Text. But I don’t want to convert you. In fact, converting programmers to your favorite IDE is usually futile. I think my co-worker Orion would have had better luck converting our neighbors in section 302 of Citi Field to the Giants a few weeks ago.

When I joined Greenhouse, I switched to Rubymine for about a year. Many developers love it — it’s powerful, truly a complete development tool with a ton of functionality out of the box. But Rubymine is a lot more resource hungry than Sublime, and I just prefer to do command line tasks — such as running specs or committing changes — from the terminal.

So I switched back to Sublime. Below are a few hints to configuring Sublime to be a solid environment for developing Rails. This article is not a Sublime primer — I’m assuming you already use it. Rather, it’s a list of a few features I saw in Rubymine that require configuration or a package to work in Sublime.

Click to definition

By default, Sublime Text 3 indexes definitions of a variable, method, class or module. Any doubt, just make sure indexing is set to true in your user settings. But you really want a key shortcut here. Check out the list of key bindings below.

User Settings{
"index": true,
"find_selected_text": true,
}
User Key Bindings
[
// git
{ "keys": ["alt+ctrl+g"], "command": "git_blame" },

// goto
{ "keys": ["alt+d"], "command": "goto_definition" },
{ "keys": ["super+shift+t"], "command": "rails_go_to_spec" },
// copy
{ "keys": ["ctrl+alt+i"], "command": "copy_path" },
// sidebar
{ "keys": ["super+2"], "command": "toggle_side_bar" },
{ "keys": ["super+1"], "command": "reveal_in_side_bar" },
// toggle autosave
{ "keys": ["ctrl+shift+s"], "command": "auto_save" },
]

Find selected text

Configure Sublime to plug highlighted text into a Find box. Just set find_selected_text to true in User Settings.

Ruby block completion

Although there are a few Ruby packages on Sublime, none solved the simple problem of autocompleting blocks or method definitions with the end keyword. This annoyed me, and then some. The below snippet more or less solves the problem.


Ruby autocomplete snippet `module…end`. Can modify for `def..end`, `class…end`, etc

<snippet>
<content><![CDATA[
module
${1:# use dob for module with variable}
end
]]></content>
<tabTrigger>module</tabTrigger>
<scope>source.ruby</scope>
<description>module..end (no variable)</description>
</snippet>

Recognize ruby predicates or bangs

Another frustration is that Sublime will not recognize Ruby predicates out of the box. Rather, it thinks the trailing question mark or exclamation is a word separator, and throws off your searching. You can fix this problem by configuring your Ruby syntax settings.

Recognize predicate methods
Tell Sublime to include trailing ? and ! characters in word selections:
Add the following property to the Ruby syntax-specific settings. To get to the Ruby settings, open a Ruby file in the editor and then navigate the menus to Sublime Text → Preferences → Settings — More → Syntax Specific — User.
{
"word_separators": "./\\()\"'-:,.;<>~@#$%^&*|+=[]{}`~"
}

Copy file path

I use this feature frequently to run specs or use Git commands. Just make a custom key binding. Sublime has none out of the box.

Find or create spec file

Rubymine has an ingenious key shortcut that will either find or create a spec based on an app file path. So if you are in app/controllers/people_controller.rb, the shortcut will either take you to spec/controllers/people_controller_spec.rb, or create a new file with the right name and file path. Just install the RailsGoToSpec package, and make a key binding.

Reveal in side bar

Also a nice feature Rubymine does out of the box. In Sublime, you need a key binding that takes you a file’s place in the directory tree sidebar. Incredibly useful.

Git blame

Although I prefer to run most Git commands from the console, I do find it very useful to Git blame changes in the text editor. With the Git package installed (and, you guessed it, a key binding), you can click in a file to open a new file showing the developer and commit SHA behind each line change.

Packages

I’ve tried and uninstalled a bunch of Sublime packages. Below are the packages I actually use:

* PackageControl
* SublimeLinter (plus plugins for Ruby, Rubocop, ESLint)
* RailsGoToSpec
* MarkdownPreview
* Git
* SideBarEnhancements
* AutoSave
* CaseConversion
* ChangeQuotes

Other stuff

I found a few other resources useful when setting up Sublime 3 for Rails. Check them out:

Adam Barcan is a software engineer at Greenhouse.

Come work with us. We’re hiring!

--

--

Adam Barcan
In the weeds

Software engineer @ Greenhouse + lifelong competitive runner.