tags:

views:

158

answers:

1
1  #include <stdio.h>

2  int main(int argc, char* argv[]) {

3    int i=10;

4  /* } */

5    return 0;

6  }

If the cursor is on line 2 under '{' and I press the '%' key, the cursor shows the matching closing brace on line 4 (which is commented out) and not the matching closing brace on line 6.

Is there any workaround for this?

Thanks,

Naga Kiran

+2  A: 

You want the matchit plugin. It extends the % command in many ways, including skipping comments. Included with Vim but not enabled by default.

From it's docs:

By default, words inside comments and strings are ignored, unless the cursor is inside a comment or string when you type "%".

I also just tried on your example and it works perfectly.

gravious
I'm curious. I use matchit for filetypes like sh and html, but I'm hesitant to use it as a global plugin. Completely replacing the behavior of % doesn't sit right with me. I do like the idea of it skipping commented code. Any thoughts on this?
Jeremy Cantrell
I don't see a disadvantage in having it global. It doesn't remove any % functionality, just extends it. From `:h matchit`, if b:match_words is undefined you have standard % with comment skipping. Most ftplugins set b:match_words, if you don't want it for a filetype try an autocmd to unset in vimrc
gravious