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!
In ruby, a block is always closed with the
end keyword. Ruby blocks may be opened using one of several keywords, including
do. The rubyblock plugin is aware of each of these.
In the example image below, suppose your cursor was positioned on the word
var would enable visual mode, selecting all of the method definition.
Whereas if you typed
vir, everything inside of the method definition would be selected:
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.
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.
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:
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!
If you notice any unexpected behaviour, I would appreciate if you could report it on the github issue tracker for this project.