Vim: Highlight keyword pairs in Ruby (def/end, do/end, etc)
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 w开发者_JAVA百科as 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?
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.
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.
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.
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
精彩评论