Vim Tips Wiki
(temporary edit (will undo) - test using "original author")
(undo my last edit which was a temporary test of an alternate header)
Line 1: Line 1:
  +
{{TipImported
{{Navigation|6|4|7}}
 
  +
|id=6
'''created''' 2001
 
  +
|previous=4
· '''complexity''' basic
 
  +
|next=7
· '''original author''' Yegappan
 
 
|created=2001
· '''version''' 6.0
 
 
|complexity=basic
 
  +
|author=Yegappan
[[Category:VimTip]][[Category:Moving]]
 
  +
|version=6.0
----
 
  +
|rating=290/104
  +
|category1=Moving
  +
|category2=
  +
}}
 
==% key==
 
==% key==
 
The <tt>%</tt> key can be used:
 
The <tt>%</tt> key can be used:
Line 64: Line 68:
 
*With the cursor inside a block, type <tt>viB</tt> (inner block) or <tt>vaB</tt> (a block, including the braces).
 
*With the cursor inside a block, type <tt>viB</tt> (inner block) or <tt>vaB</tt> (a block, including the braces).
 
*Using a mouse, double-click a brace.
 
*Using a mouse, double-click a brace.
 
----
 
'''Please do not edit this page (your edits will be lost).'''
 
 
This page shows how tip 6 would appear (with ad) if the header changed "author" to "original author". This is just for an experiment, and I will undo the edit in a week or so (see [[User_talk:JohnBeckett|discussion]]). --[[User:JohnBeckett|JohnBeckett]] 04:11, 19 February 2009 (UTC)
 
   
 
----
 
----

Revision as of 07:40, 21 July 2009

Tip 6 Printable Monobook Previous Next

created 2001 · complexity basic · author Yegappan · version 6.0


% key

The % key can be used:

  • To jump to a matching opening or closing parenthesis, square bracket or a curly brace: ([{}])
  • To jump to start or end of a C-style comment: /* */.
  • To jump to a matching C/C++ preprocessor conditional: #if, #ifdef, #else, #elif, #endif.
  • To jump between appropriate keywords, if supported by the ftplugin file, for example, between begin and end in a Pascal program.

A nice keymap is:

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/C++ method/class: go to opening/closing brace, and type %=

showmatch

The showmatch option is also useful: it can reduce the need for %, the cursor will briefly jump to the matching brace when you insert one.

To speed things up, you can set the 'matchtime' option. In vimrc:

:set showmatch
:set matchtime=3

The 'showmatch' option will not scroll the screen. To scroll the screen you can use a mapping like:

inoremap } }<Left><c-o>%<c-o>:sleep 500m<CR><c-o>%<c-o>a
inoremap ] ]<Left><c-o>%<c-o>:sleep 500m<CR><c-o>%<c-o>a
inoremap ) )<Left><c-o>%<c-o>:sleep 500m<CR><c-o>%<c-o>a

matchit

The matchit.vim script allows you to configure % to match more than just single characters. You can match words and even regular expressions. Also, matching treats strings and comments (as recognized by the syntax highlighting mechanism) intelligently.

Matchit is included in the Vim standard distribution since version 6, but it's not enabled by default.

The version at Vim scripts is usually more up to date, so you probably want that one. Unzip it in your ~/.vim directory. To use the version included with Vim copy $VIMRUNTIME/macros/matchit.vim to ~/.vim/plugin and $VIMRUNTIME/macros/matchit.txt to ~/.vim/doc.

Rebuild the help tags with :helptags ~/.vim/doc

For example, you can now use % and g% to cycle back and forth between if and fi in shell scripts.

See :help matchit-install for more information on installing matchit. After installation you can use :help matchit for usage information.

See also

Comments

You can select a block delimited by braces using any the following:

  • With the cursor on a brace, type v% to select to the matching brace.
  • With the cursor inside a block, type viB (inner block) or vaB (a block, including the braces).
  • Using a mouse, double-click a brace.