22

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.

2 Answers 2

20

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.

2
  • 1
    Note: typing :runtime macros/matchit.vim doesn't work; you need to put in in your vimrc and restart Vim... Feb 3, 2015 at 20:10
  • I did that. Still doesn't work for me :( Aug 18, 2017 at 10:32
1

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

:runtime macros/matchit.vim
filetype plugin on
1
  • 1
    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, 2017 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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