tags:

views:

153

answers:

3

I am trying to learn VIM, buy I noticed when I try using the % command to go to a matching bracket, VIM sometimes finds the correct matching bracket and is sometimes way off. Has anyone ever come across this with a solution?

A: 

No, I haven't. It might indicate you have missing brackets.

catchmeifyoutry
+4  A: 

This happens because:

  1. You have an actual syntax error with a mismatched paren or curly brace somewhere.
  2. You have a value in cpoptions that prevents semi-intelligent brace matching. Look up :help cpoptions and :help %.
  3. You have something too sophisicated for plain old Vim matching to handle. Try installing the matchit plugin, which may help. Common culprits are braces inside comments or regexes.
JSBangs
there is a 4th non-obvious option: I had a "syntax error" in a comment.
ldog
Comments were meant to be included in #3. I should have been more explicit, because braces in comments is a common scenario that I had in mind when I wrote #3.
JSBangs
A: 

Apparently, vim parses brackets even in comments, which was happening in my case, I had a non-complete bracket pair commented out, meaning vim did not find bracket pairs correctly but the compiler had no problem, hence my confusion.

ldog
Yeah, comments and regexes are the usual culprits when I have this problem. This was meant to be implied by #3 in my list.
JSBangs