Vim Tips Wiki
(Uploaded by JohnBot from a locally edited file)
(fixing category - this tip is about cursor movement, not about auto-insertion of brackets!)
Line 39: Line 39:
   
 
----
 
----
[[Category:Brackets]]
+
[[Category:Moving]]

Revision as of 02:39, 21 February 2008

Tip 6 Printable Monobook Previous Next

created February 24, 2001 · complexity basic · author Yegappan · version 5.7


The % key can be used

  1. To jump to a matching opening or closing parenthesis, square bracket or a curly brace i.e. ([{}])
  2. To jump to start or end of a C-style comment /* */.
  3. To jump to a matching #if, #ifdef, #else, #elif, #endif C preprocessor conditionals.

To get more information about this, see :help %.

The % key can be extended to support other matching pairs by modifying the "matchpairs" option. Read :help 'matchpairs'.

Comments

Also check out Benji Fisher's matchit.vim set of functions to give '%' type toggling for a multitude of language constructs, like: if,else,elseif,endif

The matchit script is now a plugin, and is included in the macros/ directory of the standard distribution of Vim 6.x. See :help add-local-help for installation details.


I also find very useful the following map:

noremap % v%

This way, whenever you type "%" you jump to the matching object AND you visually select all the text in between.

It's useful for indenting a C or C++ method/class: go to opening/closing {/}, hit % and then hit = That's it!


Don't forget 'showmatch', it can reduce the need for %. I like to speed things up a bit so I also set 'matchtime'. In .vimrc:

:set showmatch
:set matchtime=3