Power up your Sublime Text as Ruby/Rails IDE

by Sudhagar S.

“Manners maketh man”.

and

“Tools maketh developer”.

Every programmer’s most basic tool is the editor that he uses to write code. The oldest software rivalry says a lot about how editors form a base of everything we do.

After my failed attempts with Vim, I decided to stick with Sublime Text and make it so powerful that I don’t have to think about any other editor anymore. As a regular user of Sublime Text, you might already be using plugins like Package Control, SideBarEnhancements, SublimeLinter, AllAutoComplete and few themes.

Here’s a list of the plugins you should install, the tweaks you should do and the tricks you should know for Sublime Text to function like an IDE.

Plugins

1. BeautifyRuby:

Code that is aesthetically pleasing is easy to read and therefore easy to understand.

BeautifyRuby makes sure your code is well formatted. It maps the beautify_ruby command to cmd+ctrl+k by default. If you want to customise it, put the following entry in user key bindings. I use cmd-shift-k.

{
“keys”: [“super+shift+k”],
“command”: “beautify_ruby”
}

2. BracketHighlighter

When your code is having more than 3 levels of indentation, it is chaos. This plugin highlights the enclosing/respective if..end, do..end, def..end, class..end, thus making your navigation easier.

3. ProductiveSnippetsRuby, Ruby On Rails Snippets

Though Sublime Text provides basic code snippets, this plugin takes them to the next level. My favourite snippets are ‘b-p’ for binding.pry, ‘d-o-zero’ for no argument blocks and ‘d-o-tab’ for blocks that take a argument.

Creating snippets for sublime is really easy and this plugins’s repo is a great place to get started.

Here is a sample,

// ft for 'focus: true'
<snippet>
<content><![CDATA[focus: true]]></content>
<tabTrigger>ft</tabTrigger>
<scope>source.ruby</scope>
<description>focus: true</description>
</snippet>

4. RSpec

This plugin provides rspec language syntax, rspec snippets (des, con, eql, neql, etc) and the best of all — you can run your specs from the editor itself with cmd-b(build).

5. SublimeLinter

Linting helps in finding out potential errors, so have at least one linter plugin. I use SublimeLinter. There are others like SublimeLinter-rubocopand SublimeLinter-ruby.

6. SublimeREPL

With this plugin, you can spin up a pry/rails console in a new editor tab. It doesn’t work out of the box if you are using rbenv. Some plugin files have to be edited to make it work. Follow this link to get it working smooth. Also its super aggressive auto-complete can be irritating at times.

Sometimes, I have rails console running in the bottom right screen when I am on gird layout in bigger screens. This way I don’t have to go to terminal just to evaluate an expression.

7. Glue

Glue makes it possible to run terminal commands from editor. Though it is not as powerful as terminal, I use it to quickly run git status and to copy/paste/delete files.

8. Github Tools

With this plugin you can open from the editor the following things on Github,

  1. Repository home page
  2. Pull requests page
  3. Issues page
  4. Current file
  5. Blame page (i.e. commits history)

9. GitGutter

Gutter is the area in your editor where line numbers are typically displayed. This plugin shows git diff by placing an icon in the gutter area indicating whether a line has been inserted, modified or deleted.

10. Other plugins you can install based on your needs,

  1. Bootstrap
  2. Emmet
  3. HTML-CSS-JS prettify
  4. Javascript snippets
  5. SASS
  6. SCSS
  7. Ruby slim

Shortcuts

Anything you can imagine, sublime has a shortcut for that. Stop using touch pad when you are on the editor, you don’t need it.

1. Go to Definition

In IDEs like eclipse and Intellij, cmd+click on a method name at the place where it is called, takes you to its definition. This is very useful as you are juggling between your classes and debugging.

Sublime has commands goto_definition and jump_back which you can map to the keys you want. Here is how I have mapped them.

// Default (OSX).sublime-keymap
{ “keys”: [“super+down”], “command”: “goto_definition” },
{ “keys”: [“super+up”], “command”: “jump_back” }

I do cmd+down on a method call to see its implementation and cmd+up to go back again to the method call. Pretty neat, huh?

2. Variable Renaming

If you want to change a variable name, keep the cursor on the variable name and keep hitting cmd+d until it selects all the occurrences you want to edit. After you are done selecting, you can edit them all at once.

3. Swap lines

cmd+ctrl+(up|down) swaps a line with the above or below line.

4. Navigate between views

Use cmd+shift+[ and cmd+shift+] to navigate back and forth between files.

Shortcuts cmd+alt+left and cmd+alt+right also do the same thing. So pick which ever your fingers prefer.

5. Paste from history

Doing cmd+k, cmd+v opens a panel with everything you have copied so far and you can select which one to paste.

6. Go to line

You can go to a line number by doing ctrl+g and typing the line number.

You can map it to the key you want, by putting this entry in user key bindings.

{ 
“keys”: [“cmd+l”],
“command”: “show_overlay”,
“args”: {“overlay”: “goto”, “text”: “:”}
}

I use cmd+l which — by default — is mapped to extend_selection command which I don’t think I would ever use.

So that’s all I got. If all these seem overwhelming to you in the beginning, give it a week and you will notice a positive change in how write ruby code in Sublime Text. I am @shotwhat on Twitter. Let me know how this helped you.

Published on July 28, 2015.


Power up your Sublime Text as Ruby/Rails IDE was originally published in Coding Big — The Crowdfire Engineering Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

Source: Crowdfire