Tip #6: Using the % key
tip karma |
Rating 321/114, Viewed by 10701
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
February 24, 2001 17:59 |
|
complexity: |
|
basic |
author: |
|
Yegappan |
|
as of Vim: |
|
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, do
:help %
The % key can be extended to support other matching pairs by
modifying the "matchpairs" option. Read the help on
:help matchpairs
<< Quickly searching for a word |
Jumping to the start and end of a code block >>
Additional Notes
Anonymous,
February 28, 2001 11:00
|
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
|
[email protected],
April 12, 2001 9:52
|
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!
|
[email protected],
May 7, 2002 8:16
|
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.
|
[email protected],
September 21, 2002 15:53
|
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
|
[email protected],
December 5, 2002 11:05
|
If you are stuck with vim 5.7, you can use matchit.vim 1.0, available from the scripts section of this site:
https://www.vim8.org/script.php?script_id=39
|
Anonymous,
December 14, 2003 17:40
|
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
|
[email protected],
June 1, 2005 2:04
|
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?
|
Anonymous,
July 1, 2005 15:51
|
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...
|
[email protected],
May 24, 2006 22:27
|
> 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\+\)\@<!'
let s:notelse= '\(\<else\)\@<!'
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.
|
|