Ruby text objects Fri, 18 Jul 2014
Yesterday I discovered a text object within Vim that I’d not realised existed, which served as a perfect tip post up onto this blog (and to try and get back to doing it more regularly!).
Any seasoned Ruby + Vim user has probably got Drew Neil’s textobj-rubyblock plugin installed, which adds the text objects ar and ir to select around and inside a ruby block.
However, what I didn’t know about was the text object im, which is provided by vim-ruby (which comes with Vim by default), to select the contents within the current Ruby method, and its sibling am, to select around the method. Hat tip to @tomstuart for that one:
It took me a few minutes to work out how to select the body of a Ruby method in Vim, but I was happy when the answer turned out to be “vim”.
— Tom Stuart (@tomstuart) July 17, 2014
Drew Neil (@nelstrom)) then followed up explaining that it was recently added to vim-ruby:
@telemachus it’s a text object defined in the vim-ruby plugin: https://t.co/YZSmUPWr0P
Should be available out the box in recent Vim builds.
— Drew Neil (@nelstrom) July 17, 2014
If you’re running a recent version of Vim you should have this functionality, but if you’re not, you can always install the latest version of vim-ruby as a plugin through Vundle / Pathogen. The best solution though if possible is to simply upgrade your Vim.
Whilst reading to produce this blog post I also discovered the objects iM and aM, which select in and around the current Ruby class, too. All of these are going to be really useful in my day to day development.