Home Select Blocks of Ruby Code in Vim with rubyblock

Select Blocks of Ruby Code in Vim with rubyblock

rubyblock is a custom text object for Vim for selecting Ruby blocks. Drew Neil, the developer, explains: “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.”

Neil also notes a limitation:

Some text objects in Vim respond to a count. For example, the `a{` text object will select _all_ of the current `{}` delimited block, but if you prefix it with the number 2 (e.g. `v2i{`) then it will select all of the block that contains the current block. The rubyblock text object does not respond in this way if you prefix a count. This is due to a limitation in vimscript #2100.

However, you can achieve a similar effect by repeating the rubyblock text-object manually. So if you press `var` to select the current ruby block, you can expand your selection outwards by repeating `ar`, or contract your selection inwards by repeating `ir`.

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the gambling and blockchain industries for major developments, new product and brand launches, game releases and other newsworthy events. Editors assign relevant stories to in-house staff writers with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.