views:

287

answers:

3

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?

A: 

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
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
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
A: 

This is kind of a hack, but you could do a regex search like

/def.+end

This won't match things on multiple lines, but I'm sure that wouldn't be hard to do. I'm sure it's in "help" somewhere. you could search for multiple of them, stick it in a script file, and then map it to a key command and stick the map in your _vimrc (or .vimrc if you're a linuxer ;)

Obviously, this woudn't give you a specific match, just the next one, so I don't know if that would help with "heavily-nested" stuff.

danoran
+1  A: 

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