Check out the amazing Black Friday deals on my live coding videos at Peer to Peer.

Get up to 50% off!

Check out the amazing Black Friday deals on my live coding videos at Peer to Peer.

A text-object for ruby blocks

Ever since learning about Vim’s text-objects I have wished that there was a way of selecting blocks in ruby code. Well, now there is. Today I am releasing a plugin that creates a custom text object for selecting ruby blocks. Merry Christmas!

Usage

In ruby, a block is always closed with the end keyword. Ruby blocks may be opened using one of several keywords, including module, class, def if and do. The rubyblock plugin is aware of each of these.

In the example image below, suppose your cursor was positioned on the word def. Typing var would enable visual mode, selecting all of the method definition.

The 'var' text-object selects all of the current ruby block

Whereas if you typed vir, everything inside of the method definition would be selected:

The 'vir' text-object selects inside of the current ruby block

If you make a visual selection, you can expand it to encompass the outer ruby block by pressing ar, or you can contract your selection by pressing ir. You can repeat this as many times as you like. The image below begins with the innermost ruby block selected. By pressing ar again and again, the selection expands until it encompasses all of the outermost block. If you press ir, you can back up the other way, contracting the selection towards inner blocks.

You can expand or contract your selection by repeating 'ar' and 'ir' commands

Installation

If you like to keep your plugins under version control with pathogen, then you will want to clone the textobj-user and textobj-rubyblock plugins from github.

If you’re old-school, you can get the textobj-user and textobj-rubyblock plugins from vim.org.

Note that the rubyblock plugin currently requires that the matchit.vim plugin is enabled. This can be done by adding the following line to your vimrc:

runtime macros/matchit.vim

Also note, that there is a plugin called ruby-matchit, which calls itself ‘matchit for ruby’. If you have this installed, it may interfere with the operation of the rubyblock plugin. Also, matchit.vim supports ruby now (which was not the case when ruby-matchit was written), so there’s no need to install them both.

Morgan Prior took the time to explain how to install the rubyblock plugin on his blog, so go read that if my instructions are too brief. Thanks Morgan!

Issues

If you notice any unexpected behaviour, I would appreciate if you could report it on the github issue tracker for this project.

Thanks

I never thought I had the vimscript chops to put this plugin together, but the textobj-user framework made it possible. So I’d like to say a big thankyou to Kana for his excellent work.

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with Thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from Thoughtbot..