how to find matching braces in vi or vim editor

If you are using the vi or vim editor to browse code (or any text for that matter), then quite often you would want to find the matching parenthesis to determine the scope of the enclosing brackets. Most modern editors such as gvim or vim has the ability to automatically highlight the matching parenthesis when the cursor is on a bracket/brace/parenthesis.

However if the matching bracket is not visible or you want to move the cursor to the matching parenthesis, you can use the % in command mode. The % key (or command..however you want to call it) works slightly different depending on where the cursor is on the file and the determined language of the file content.

in generic text

If the cursor is on a starting parenthesis “(“, a square bracket “[” or a curly bracket “{“, then the % key will take you to the matching closing parenthesis “)]}”. If you are on the closing parenthesis or bracket then it will jump to the matching opening bracket. This can be useful in moving around the file as well.

This will work even if the cursor is not on the parenthesis. If the cursor is on a non-empty line then the % key will find next bracket on the line and then jump to the matching bracket of the same. So it is not necessary to always move the cursor to the bracket, being on the same line of text will work.

in C-style code or text

If you are viewing a C style code, then the key can be used to jump between the start or end of a C-style comment. It is also be used to move between the pre-processor conditionals found in the C/C++ code, such as #if, #ifdef, #else etc.

Depending on the type of code or the language of the file, it is quite possible that the % key can be used to jump between matching keywords. In the case of HTML files, you can use this to move between matching tags.