Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim finds incorrect matching bracket when using %

Tags:

vim

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?

like image 959
ldog Avatar asked Dec 14 '09 20:12

ldog


3 Answers

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.
like image 56
JSBձոգչ Avatar answered Nov 01 '22 21:11

JSBձոգչ


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.

like image 33
ldog Avatar answered Nov 01 '22 21:11

ldog


matchit plugin would have saved me hours of frustration if I had found it earlier.

There's a small chance pasting this ugly command into your terminal will correctly set up the plugin:

wget http://www.vim.org/scripts/download_script.php?src_id=8196; unzip download_script.php?src_id=8196; echo >> ~/.vimrc; echo -ne "source " >> ~/.vimrc; pwd | tr -d '\n' >> ~/.vimrc; echo -ne "/plugin/matchit.vim" >> ~/.vimrc; rm download_script.php\?src_id\=8196

Then re-source your vimrc:

:source ~/.vimrc

like image 2
funroll Avatar answered Nov 01 '22 20:11

funroll