32

In one of the Eclipse-based editors that I tried out recently (I think it was RubyMine), when a Ruby keyword that either opened or closed a method or block was selected, the corresponding open/close keyword was highlighted. Similar to the way that Vim is able to highlight a corresponding open/close parenthesis.

For instance, if I selected a 'def', it would highlight the corresponding 'end'. It also worked for do/end blocks.

This was really handy, especially for those long and sometimes heavily nested Rspec files.

Does anybody know how to achieve this in Vim?

pakeha
  • 2,480
  • 3
  • 22
  • 24
  • 2
    Take a look at ruby-matchit.vim - which allows you to bounce between `def`...`end` and `class`..`end` and company using `%`. The logic should be the same for what you want. http://vim.sourceforge.net/scripts/script.php?script_id=290 – Telemachus Aug 05 '10 at 11:57
  • 1
    Another useful Vim plugin for `x`...`end` pairs is Tim Pope's `endwise`, which will automagically insert an `end` whenever you start a section that requires one: http://github.com/tpope/vim-endwise – Telemachus Aug 05 '10 at 12:15
  • Thanks for the comments Telemachus. These aren't precisely what I'm looking for, but helpful nonetheless. ruby-matchit can serve the same purpose, but I would prefer the highlighting as described in my question, if possible. Once I learn more about Vim, I may investigate whether it is possible to implement myself. – pakeha Aug 05 '10 at 21:44
  • 1
    ++ tope's plugins (and I think RubyMine is based on Inteli-J not Eclipse). – Jed Schneider Aug 06 '10 at 01:21
  • Ah yes you are correct it is an Intelli-J product. I tried a few IDEs all within a few days when I was considering where to go from Textmate (RubyMine, Aptana Studio, 3rdRail, Netbeans), only to settle back on a text editor... – pakeha Aug 08 '10 at 08:15
  • vim does this for parentheses - so extending it to do/end might be possible by finding out how it does parenthesis-matching. – Taryn East Aug 10 '10 at 17:03
  • You don't have to use ruby-matchit.vim to get `%` navigation between `def` and `end`; you can use regular matchit.vim, which "allows you to configure % to match more than just single characters" and worked out of the box for me in Ruby. It also says that "Since vim 6.0, matchit.vim has been included in the standard vim distribution, under the macros/ directory", but I didn't have it. Anyway, it's here: http://www.vim.org/scripts/script.php?script_id=39 – Nathan Long Mar 08 '11 at 19:19

4 Answers4

18

If you are using Vim 7.3, you should have the MatchIt vim macro available.

Add runtime macros/matchit.vim to your .vimrc file and you should be able to use % to match the ruby blocks.

You can look at the filetype plugin for ruby to see what it will move between.

deterb
  • 3,994
  • 1
  • 28
  • 33
10

VIM (until 7.2) can't highlight a closing 'if/end' pairs because the matching settings accepts a single character (see :help matchpairs). I recommend using folding instead, provided that you accurately indent your code:

:set foldmethod=indent

Then use: zc, za to make sure you're in the right block.

Omar Ali
  • 8,467
  • 4
  • 33
  • 58
2

I found this plugin while searching for the answer on the same problem it works for basic ruby code, but I didn't test it out for Rspec etc.

Just install it via pathogen and add let g:hl_matchit_enable_on_vim_startup = 1

https://github.com/vimtaku/hl_matchit.vim

12init
  • 127
  • 8
2

Looks like this vim plugin does paren-matching: http://vimdoc.sourceforge.net/htmldoc/pi_paren.html you could probably dig into that code to see how to extend it to matching other things.

Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • 1
    it won't work since as `:help matchpairs` says: Currently only single byte character pairs are allowed, and they must be different. – KARASZI István Aug 17 '10 at 14:36
  • Yes, it says currently... but the code will give an idea of how to do paren-matching, and then you can extend it so that it will support multi-char pairs yes? It's a place to start :) – Taryn East Aug 18 '10 at 10:25