Vim Tips Wiki
No edit summary
 
Yegappan (talk | contribs)
No edit summary
Line 1: Line 1:
{{review}}
 
 
{{Tip
 
{{Tip
 
|id=6
 
|id=6
Line 10: Line 9:
 
|text=
 
|text=
   
 
The % key can be used
   
 
# To jump to a matching opening or closing parenthesis, square bracket or a curly brace i.e. ([{}])
The % key can be used
 
 
# To jump to start or end of a C-style comment /* */.
 
# To jump to a matching #if, #ifdef, #else, #elif, #endif C preprocessor conditionals.
   
 
To get more information about this, use the commands:
   
  +
:help %
   
 
The % key can be extended to support other matching pairs by modifying the "matchpairs" option. Read the help on
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, do
 
 
 
 
[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:%}} :help %]
 
 
 
 
The % key can be extended to support other matching pairs by
 
 
modifying the "matchpairs" option. Read the help on
 
 
 
 
[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:matchpairs}} :help matchpairs]
 
   
  +
:help matchpairs
   
 
}}
 
}}
Line 53: Line 32:
 
http://sites.netscape.net/BBenjiF/vim
 
http://sites.netscape.net/BBenjiF/vim
   
'''Anonymous'''
+
Anonymous, February 28, 2001 11:00
, February 28, 2001 11:00
 
 
----
 
----
 
I also find very useful the following map:
 
I also find very useful the following map:
Line 63: Line 41:
 
It's useful f.i. for indenting a C or C++ method/class: go to opening/closing {/}, hit % and then hit = That's it!
 
It's useful f.i. for indenting a C or C++ method/class: go to opening/closing {/}, hit % and then hit = That's it!
   
 
lacaprara--AT--pd.infn.it, April 12, 2001 9:52
 
lacaprara--AT--pd.infn.it
 
, April 12, 2001 9:52
 
 
----
 
----
 
The matchit script mentioned in the first note is now a plugin,
 
The matchit script mentioned in the first note is now a plugin,
Line 75: Line 51:
 
for installation details.
 
for installation details.
   
benji--AT--member.AMS.org
+
benji--AT--member.AMS.org, May 7, 2002 8:16
, May 7, 2002 8:16
 
 
----
 
----
 
the link to the matchit script apparently is no longer valid -- neither IE nor Netscape can find the server. Is there a newer link for those of us whose institutions (Univ. of Michigan....) are still stuck on v5.7?
 
the link to the matchit script apparently is no longer valid -- neither IE nor Netscape can find the server. Is there a newer link for those of us whose institutions (Univ. of Michigan....) are still stuck on v5.7?
Line 82: Line 57:
 
thanks
 
thanks
   
vimonline--AT--dangercat.net
+
vimonline--AT--dangercat.net, September 21, 2002 15:53
, September 21, 2002 15:53
 
 
----
 
----
 
If you are stuck with vim 5.7, you can use matchit.vim 1.0, available from the scripts section of this site:
 
If you are stuck with vim 5.7, you can use matchit.vim 1.0, available from the scripts section of this site:
 
http://www.vim.org/script.php?script_id=39
 
http://www.vim.org/script.php?script_id=39
   
benji--AT--member.AMS.org
+
benji--AT--member.AMS.org, December 5, 2002 11:05
, December 5, 2002 11:05
 
 
----
 
----
 
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:
 
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 showmatch
set matchtime=3
+
:set matchtime=3
   
'''Anonymous'''
+
Anonymous, December 14, 2003 17:40
, December 14, 2003 17:40
 
 
----
 
----
 
it seems % cannot cope up with commented {}. so if, i have following
 
it seems % cannot cope up with commented {}. so if, i have following
   
something {
+
something {
  +
// something commented {
+
// something commented {
} //end
 
  +
 
} //end
   
 
pressing % on "end" brace will match "something commented" than "something".
 
pressing % on "end" brace will match "something commented" than "something".
Line 109: Line 83:
 
anybody knows a workout for that?
 
anybody knows a workout for that?
   
kumarpindia--AT--yahoo.com
+
kumarpindia--AT--yahoo.com, June 1, 2005 2:04
, June 1, 2005 2:04
 
 
----
 
----
 
Well, you can either make a script or put a 'fixme: }' comment... like:
 
Well, you can either make a script or put a 'fixme: }' comment... like:
   
something {
+
something {
  +
// something commented {
+
// something commented {
//FIXME: }
 
  +
 
//FIXME: }
  +
 
} //end
 
} //end
   
 
FIXME will always be highlighted, so it will be harder to forget about it...
 
FIXME will always be highlighted, so it will be harder to forget about it...
   
'''Anonymous'''
+
Anonymous, July 1, 2005 15:51
, July 1, 2005 15:51
 
 
----
 
----
 
 
Line 140: Line 115:
 
I appended the following line
 
I appended the following line
   
if exists("loaded_matchit") && !exists("b:match_words")
+
if exists("loaded_matchit") && !exists("b:match_words")
  +
let b:match_ignorecase = 0
+
let b:match_ignorecase = 0
"let s:notelse= '\%(\<else\s\+\)\--AT--<!'
 
  +
let s:notelse= '\(\<else\)\--AT--<!'
 
let b:match_words = s:notelse. '\<if\>:\<else\s\+if\>\|\<else\>,{:},(:),try:catch'
+
"let s:notelse= '\%(\<else\s\+\)\--AT--<!'
  +
 
let s:notelse= '\(\<else\)\--AT--<!'
  +
  +
let b:match_words = s:notelse. '\<if\>:\<else\s\+if\>\|\<else\>,{:},(:),try:catch'
  +
 
endif
 
endif
   
 
it seems to work alright for me. it is just something quick I came up with, hope it work for you too.
 
it seems to work alright for me. it is just something quick I came up with, hope it work for you too.
   
 
pekcheey--AT--gmail.com, May 24, 2006 22:27
 
pekcheey--AT--gmail.com
 
, May 24, 2006 22:27
 
 
----
 
----
 
<!-- parsed by vimtips.py in 0.480768 seconds-->
 
<!-- parsed by vimtips.py in 0.480768 seconds-->

Revision as of 19:51, 7 June 2007

Previous TipNext Tip

Tip: #6 - Moving to matching braces

Created: February 24, 2001 17:59 Complexity: basic Author: Yegappan Version: 5.7 Karma: 290/104 Imported from: Tip#6

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, use the commands:

:help %

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

: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

http://sites.netscape.net/BBenjiF/vim

Anonymous, February 28, 2001 11:00


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 f.i. for indenting a C or C++ method/class: go to opening/closing {/}, hit % and then hit = That's it!

lacaprara--AT--pd.infn.it, April 12, 2001 9:52


The matchit script mentioned in the first note is now a plugin, and it is included in the macros/ directory of the standard distribution of vim 6.x. See

:help add-local-help

for installation details.

benji--AT--member.AMS.org, May 7, 2002 8:16


the link to the matchit script apparently is no longer valid -- neither IE nor Netscape can find the server. Is there a newer link for those of us whose institutions (Univ. of Michigan....) are still stuck on v5.7?

thanks

vimonline--AT--dangercat.net, September 21, 2002 15:53


If you are stuck with vim 5.7, you can use matchit.vim 1.0, available from the scripts section of this site: http://www.vim.org/script.php?script_id=39

benji--AT--member.AMS.org, December 5, 2002 11:05


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

Anonymous, December 14, 2003 17:40


it seems % cannot cope up with commented {}. so if, i have following

something {

// something commented {

} //end

pressing % on "end" brace will match "something commented" than "something".

anybody knows a workout for that?

kumarpindia--AT--yahoo.com, June 1, 2005 2:04


Well, you can either make a script or put a 'fixme: }' comment... like:

something {

// something commented {

//FIXME: }

} //end

FIXME will always be highlighted, so it will be harder to forget about it...

Anonymous, July 1, 2005 15:51


> it seems % cannot cope up with commented {}. so if, i have following > > something { > // something commented { > } //end > > pressing % on "end" brace will match "something commented" than "something". > > anybody knows a workout for that?

I'm facing the same problem as well, my solution for it is for always use matchit.vim for my code. even java.

Basically in my C:/Program Files/Vim/vim70/ftplugin/java.vim I appended the following line

if exists("loaded_matchit") && !exists("b:match_words")

let b:match_ignorecase = 0

"let s:notelse= '\%(\<else\s\+\)\--AT--<!'

let s:notelse= '\(\<else\)\--AT--<!'

let b:match_words = s:notelse. '\<if\>:\<else\s\+if\>\|\<else\>,{:},(:),try:catch'

endif

it seems to work alright for me. it is just something quick I came up with, hope it work for you too.

pekcheey--AT--gmail.com, May 24, 2006 22:27