17

In C and C-like languages, I can use % to jump to the corresponding curly brace that the cursor is on. This is a well-known "trick".

But in Ruby for example:

def fun
    [1, 2].each do |n|
    end
end 

This doesn't work, since ruby doesn't use the characters in matchpairs (set to (:),{:},[:],<:> by default.

I tried setting that, but it doesn't work:

:set matchpairs=def:end
E474: Invalid argument: matchpairs=def:end

Can I get this to work with languages such as Ruby as well? Note this is not a Ruby-specific question, other examples might be shell scripts (if/fi) or Lua (function/end), and many many more.

17

You can use the matchit plugin. This is included in modern vim distributions, so all you have to do to use it is add the following to your vimrc:

runtime macros/matchit.vim

You can also get it packaged as a plugin if you prefer. It recognizes many keywords by default (including def and end) and can be extended to recognize more.

  • 1
    Note: typing :runtime macros/matchit.vim doesn't work; you need to put in in your vimrc and restart Vim... – Martin Tournoij Feb 3 '15 at 20:10
  • I did that. Still doesn't work for me :( – Thirupathi Thangavel Aug 18 '17 at 10:32
0

The following doc matchit, the the additions below in ~/.vimrc worked for me

:runtime macros/matchit.vim
filetype plugin on
  • Welcome to the site! This is basically the same answer as the existing, accepted answer. The difference is your filetype command: it might be helpful if you described in detail why you've added this line and how it helps answer the question. (You've also got a couple of typos: "doc", "the the".) – Rich Oct 27 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.