5
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?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Naga Kiran
  • 8,585
  • 5
  • 43
  • 53

1 Answers1

8

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
  • 467
  • 2
  • 4
  • 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 Apr 13 '09 at 16:31
  • 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 Apr 13 '09 at 22:09