I'm using set matchpairs+=<:> with my C++ code, and it works great. But there are some cases where it is not quite there, like this C++ code:

#include <type_traits>
template<int X> typename std::enable_if<(X>9),int>::type func() { return 5; }
int main() { func<10>(); }

Here, if you put the cursor on the < after enable_if, the > in X>9 will be highlighted. Of course it makes sense from the perspective of Vim, which isn't a C++ compiler nor an IDE. But is there any easy way to make it not match pairs broken by non-closed parentheses? I.e. I'd like <()> to match the closing angle bracket, and <(>) not to.

  • just curious, are you working with matchit ? github.com/tmhedberg/matchit, since it often solves this kind of problems, but i am not sure if it will help in your case
    – B.G.
    Commented Feb 13, 2017 at 15:05
  • @DoktorOSwaldo no, just plain vim.
    – Ruslan
    Commented Feb 13, 2017 at 15:43

1 Answer 1


matchit will most probably not do this for you. You probably want to write a small wrapper function for %, which identifies the character under your cursor with:

let curChar = char2nr(matchstr(getline('.'), '\%' . col('.') . 'c.'))

followed by a couple of simple nested tests such that:

test-1: if curChar is not `<`, then
     act simply the way `%` would
    test-2:  if whatever lies between current `<` and following `>` contains one of ( or ) or { or } or [ or ], then
        do not jump or highlight
        jump or highlight

which means , get scripting in vim ...

Let us know how this goes.

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.