Vim for Rubyists Part 1

Ruby and Vim Photo by supervillain

It is really exciting to be a Rubyist lately. We have our amazing tools (rake, rspec, Cucumber, Rails etc.), we write object-oriented code, that is easy to maintain and flexible when it comes to adding new features, and changing domain logic. We have amazing hosting support from Heroku, Engine Yard and Rackspace. And last, but not least, powerful monitoring tools like New Relic are ready to tell us when there are issues in our applications. But do we have the perfect editor? A lot of people are still using TextMate, some of them started the journey of Sublime Text 2 and others made different choices (Emacs, RubyMine, TextMate 2 etc). And how about Vim? Do we have healthy community of Rubyists in the Vim world? Is it worth giving it a try? Let’s find out.

Rubyists using Vim

Big percentage of the Rubyists are actually using Vim. Here are some of the most popular Vim evangelists out there:

Gary Bernhardt - Gary is the creator of Destroy all Software. Watching him in action with Vim is really inspirational. Subscribe to his screencasts and you will find out what we are talking about. You can learn a lot of useful Vim tricks by watching him write Ruby code.

Aaron Patterson (tenderlove) - Tenderlove is a Ruby and Rails core team member. He is super positive person, spending a lot of his time making Rails and Ruby faster. So, thank you for that Aaron! Also, it’s a well-known fact, that he prefers Vim over other editors. You can watch him using Vim in this PeepCode Play by Play episode.

Mislav Marohnić - Mislav is a software craftsman from Croatia. He is contributing to Zepto and Rails among others. He wrote the famous “Vim: revisited” blog post, that inspired many people to start using Vim.

(Your name here? Are you a Rubyist using Vim? Send us your short introduction + links to your twitter account and dot files and we will feature you in the post)

Vim plugins for Ruby development

Vim Ruby

Vim Ruby is probably the most popular Vim plugin for Ruby development. It can be found in pretty much every vimrc owned by a Rubyist. It comes with mappings that make editing Ruby code much easier.

1
imap <S-CR> <CR><CR>end<Esc>-cc

This enables:

1
if name == "John"[<--cursor]

And when you press shift + enter:

1
2
3
if name == "John"
  [<--cursor]
end

(Note: you can accomplish the same behaviour with endwise even without pressing shift).

Files with a filename extension of .rb or a ruby shebang line (#!/usr/bin/ruby) will be set to filetype “ruby”. It brings mappings, options, matchit configuration etc.

If Vim Ruby is not in your Vim configurations you should definitely consider including it.

Vim Rails

Oh, Rails! Yes! Tim Pope did an amazing job with Vim Rails. It’s probably the most useful plugin we’ve ever used. If you are a Rails developer, you will get a lot benefits just by installing it. Here are some useful examples:

Easy navigation of the Rails directory structure

You can easily jump to alternative files, for example to specs from production code, by typing :A or :AV if you prefer to open it in a split window. Also, if you want to jump from a controller action to the view file or vice versa, you can press :R. Navigation between models, controllers and views is getting even easier with the following commands: :Rcontroller controller-name, :Rmodel model-name etc.

Enhanced syntax highlighting

The plugin adds syntax highlighting for has_and_belongs_to_many, has_many etc. Better completion is provided as well.

Interfaces to rake & Rails scripts

You can use :Rake to run the current spec or feature. :Rscript is available for convenient execution of scripts without leaving your favourite editor.

Partial extraction & migration inversion

:Rextract file can extract the current visual selection to file and replace it with render partial: 'file'.

Ruby Refactoring

Ruby Refactoring is a plugin written by Enrique Comba Riepenhausen and inspired by some Ruby refactoring patterns that Gary Bernhardt presented in the Software Craftsmanship User Group UK.

You can easily add new parameters, inline temporary variables, extract constants and more. Here is the full list of supported commands:

1
2
3
4
5
6
7
8
:RInlineTemp             - Inline Temp
:RConvertPostConditional - Convert Post Conditional
:RExtractConstant        - Extract Constant
:RExtractLet             - Extract to Let (Rspec)
:RExtractLocalVariable   - Extract Local Variable
:RRenameLocalVariable    - Rename Local Variable
:RRenameInstanceVariable - Rename Instance Variable
:RExtractMethod          - Extract Method

For most of the commands you will need to install matchit.vim as well.

Vim Cucumber

Who doesn’t use Cucumber these days? We spend a lot of time writing acceptance tests, so we need an environment that is going to help us to focus and refactor them better.

Vim Cucumber provides syntax highlighting, indenting, and a filetype detection for Cucumber. You can use CTRL-] on a step to jump to its Ruby definition or, if you prefer splits, you can use CTRL-W-], like we do. Pretty useful, right?

Vim Haml/Slim/Sass

As Ruby developers we always use the latest and the greatest. It’s all about writing beautiful code that is easy to maintain, isn’t it? That’s why we use Haml, Slim and Sass. But Haml without syntax highlighting isn’t really useful. Fortunately, there are a few plugins that will help you using your favourite frontend tools:

  • vim-haml - runtime files for Haml and Sass.
  • vim-slim - similar to vim-haml, but for Slim.
  • vim-less - Less highlighting, indenting and autocompletion.

Janus

If you are just starting with Vim, coming from TextMate or other editor, you can give Janus a shot. It’s a distribution of plugins and mappings for Vim, Gvim and MacVim. It’s bundled with a lot of useful tools for Ruby development and designed to reduce the learning curve of Vim. Apart from that, the community behind it did an amazing job by making it extendable, so you can include your own configurations and plugins with no effort. Gundo, ctrlp, easymotion, supertab, surround are just a few examples that come with Janus.

And even more

Apart from the plugins and the resources mentioned in the article, here is a list of some useful links to resources, that will make you even more productive Rubyist:

Conclusion

There are a lot of Rubyists doing web development with Vim. Besides being ruby evangelists, they also contribute to a huge amount of Vim plugins. So, if you are going to migrate to Vim, you are in a good company.

What’s your favourite Vim plugin for Ruby? Do you have any special Vim tricks for writing Ruby code? Which Vim features had the greatest influence on you? Let us know in the comments.

About the author

Veselin Todorov is a software consultant focused on testing, code quality and building awesome products. He is helping companies to ship their products with confidence and style.

If you are passionate about JavaScript, Node.js, TDD, building and growing applications, you should visit his blog at RobustJS.com