Working with Ruby
Occasional blog posts about Ruby updates, tools, editor tweaks, and random snippets. You might also be interested in my newer project that docuements lesser-known features in Ruby: Idiosyncratic Ruby.

Be More Productive with Better Sublime Snippets for Ruby!

Long time readers of this blog will remember that I used to tweak GNOME’s gedit editor a lot. However, I ditched it for Sublime Text and I am not looking back. Here are some of the things I like about Sublime:

  • Offers a good out-of-the-box experience, including an amazing “fuzzy search” tool (ctrl+p)
  • It works on ubuntu without any problems
  • Fast (enough)
  • Multiple cursors!
  • Once you have installed Package Control: An integrated extension repository
  • Encourages you to write your own extensions
  • Looks good

Some of the things I dislike about Sublime:

  • Bad syntax highlighting for Ruby (more on this in a later blog post)
  • Default support for programming languages is often broken or outdated
  • Not happy with the default snippets for Ruby and ERB

Introducing “Productive Snippets for Ruby”

The stock Ruby snippets are not particularly bad. But I had problems with the scope they cover. For example, they provide a snippet for “transaction”, which is very Railsish. Also, a lot of snippets for Test::Unit are included. It has workarounds for 1.8 (e.g. getting singleton class), which are not needed any more (2.0 is the oldest officially support Ruby version). And all in all, you will notice a different style across all snippets.

To repeat myself: I am not saying the default snippets suck. What I want ist to have a consistent selection of snippets with more “modern” idioms. And without support for specific gems, other than rake or bundler. This is, what the ProductiveSnippetsRuby package will give you. You can install it via sublime package control.

Removing the Default Snippets

The snippets are meant as a replacement for the default ones, but there is no simple way to remove them. You have to do it manually (at your own risk, of course):

  • Find out where your sublime system packages folder is. This differs depending on your operationg system and how you installed sublime. For example, on my ubuntu machine it is /opt/sublime_text/Packages (not ~/.config/sublime-text-3/Packages).
  • Look for a file called Ruby.sublime-package and backup it
  • Then unzip the file into an extra directory and change into that directory
  • Delete all *.sublime-snippet files (or keep some that you like – for example ProductiveSnippetsRuby does currently not have snippets for all the Enumerable methods).
  • zip together the rest, call it Ruby.sublime-package again and move it to the location of the original file

ERB

The default behaviour in ERB files is wrong, because some tab triggers (e.g. if) generate php code. This is caused by an error in the PHP.sublime-package. If you don’t use PHP, the simplest way to get rid of the wrong snippets, will be to just delete the PHP package (or move it away). You can find it in the same directory where the Ruby.sublime-package is (see last paragraph).

Then install some Rails-free ERB snippets or use this alternative.

Creative Commons License