Vim Tips Wiki
m (→‎showmatch: Merged from 200)
(9 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
|previous=4
 
|previous=4
 
|next=7
 
|next=7
|created=February 24, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=Yegappan
 
|author=Yegappan
|version=5.7
+
|version=6.0
 
|rating=290/104
 
|rating=290/104
  +
|category1=Moving
  +
|category2=
 
}}
 
}}
 
[[Category:Moving]]
 
 
 
==% key==
 
==% key==
The <tt>%</tt> key can be used:
+
The <code>%</code> key can be used for the following:
* To jump to a matching opening or closing parenthesis, square bracket or a curly brace i.e. <tt>([{}])</tt>
+
*To jump to a matching opening or closing parenthesis, square bracket or a curly brace: <code>([{}])</code>
* To jump to start or end of a C-style comment <tt>/* */</tt>.
+
*To jump to start or end of a C-style comment: <code>/* */</code>.
* To jump to a matching <tt>#if</tt>, <tt>#ifdef</tt>, <tt>#else</tt>, <tt>#elif</tt>, #endif</tt> C/C++ preprocessor conditionals.
+
*To jump to a matching C/C++ preprocessor conditional: <code>#if</code>, <code>#ifdef</code>, <code>#else</code>, <code>#elif</code>, <code>#endif</code>.
  +
*To jump between appropriate keywords, if supported by the ftplugin file, for example, between <code>begin</code> and <code>end</code> in a Pascal program.
   
 
A nice keymap is:
 
A nice keymap is:
Line 23: Line 23:
 
</pre>
 
</pre>
   
This way, whenever you type <tt>%</tt> you jump to the matching object AND you visually select all the text in between.
+
This way, whenever you type <code>%</code> 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 <code>%=</code>
It's useful for indenting a C/C++ method/class: go to opening/closing brace, and hit <tt>%=</tt>
 
   
 
==showmatch==
 
==showmatch==
The <tt>showmatch</tt> option is also useful, it can reduce the need for <tt>%</tt>, the cursor will briefly jump to the matching brace when you insert one.
+
The <code>showmatch</code> option is also useful: it can reduce the need for <code>%</code>, the cursor will briefly jump to the matching brace when you insert one.
 
I like to speed things up a bit so I also set <tt>'matchtime'</tt>. In vimrc:
 
   
 
To speed things up, you can set the <code>'matchtime'</code> option. In [[vimrc]]:
 
<pre>
 
<pre>
 
:set showmatch
 
:set showmatch
Line 36: Line 34:
 
</pre>
 
</pre>
   
'showmatch' will not scroll the screen, if you want to scroll the screen you can use a keybind like:
+
The <code>'showmatch'</code> option will not scroll the screen. To scroll the screen you can use a mapping like:
 
<pre>
 
<pre>
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
+
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
 
</pre>
 
</pre>
   
 
==matchit==
 
==matchit==
The matchit.vim script allows you to configure % to match more than just
+
The <code>matchit.vim</code> script allows you to configure <code>%</code> 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.
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.
+
Matchit is included in the Vim standard distribution since version 6, but it's not enabled by default.
   
{{script|id=39|text=The version at Vim scripts}} is usually more up to date, so you probably want that one, unzip it in your <tt>~/.vim</tt> directory.<br />
+
{{script|id=39|text=The version at Vim scripts}} is usually more up to date, so you probably want that one. Unzip it in your <code>~/.vim</code> directory.
To use the version included with Vim copy <tt>$VIMRUNTIME/macros/matchit.vim</tt> to <tt>~/.vim/plugin and $VIMRUNTIME/macros/matchit.txt</tt> to <tt>~/.vim/doc</tt><br />
+
To use the version included with Vim copy <code>$VIMRUNTIME/macros/matchit.vim</code> to <code>~/.vim/plugin</code> and <code>$VIMRUNTIME/macros/matchit.txt</code> to <code>~/.vim/doc</code>.
Rebuild help tags with <tt>:helptags ~/.vim/doc</tt>
 
   
 
Rebuild the help tags with <code>:helptags ~/.vim/doc</code>
For example, you can now use <tt>%</tt> and <tt>g%</tt> to cycle back and forth between <tt>if</tt> and <tt>fi</tt> in shell scripts.
 
   
 
For example, you can now use <code>%</code> and <code>g%</code> to cycle back and forth between <code>if</code> and <code>fi</code> in shell scripts.
See {{help|matchit-install}} for more information on installing matchit, after installation you can use <tt>:help matchit</tt> for for information about using matchit.
 
   
 
See {{help|matchit-install}} for more information on installing matchit. After installation you can use <code>:help matchit</code> for usage information.
==See Also==
 
  +
* {{help|%}}
 
 
==See also==
* {{help|'matchpairs'}}
 
* {{help|'showmatch'}}
+
*{{help|%}}
 
*{{help|'matchpairs'}}
* {{script|id=39|text=matchit: Extended % matching for HTML, LaTeX, and many other languages}}
 
* {{help|matchit-install}}
+
*{{help|'showmatch'}}
  +
*{{help|matchit-install}}
* {{script|id=290|text=ruby-matchit: 'Matchit' for Ruby.}}
 
 
*{{script|id=39|text=matchit: Extended % matching for HTML, LaTeX and many other languages}}
 
*{{script|id=290|text=ruby-matchit: 'Matchit' for Ruby}}
   
 
==Comments==
 
==Comments==
  +
You can select a block delimited by braces using any the following:
  +
*With the cursor on a brace, type <code>v%</code> to select to the matching brace.
  +
*With the cursor inside a block, type <code>viB</code> (inner block) or <code>vaB</code> (a block, including the braces).
  +
*Using a mouse, double-click a brace.
  +
  +
----

Revision as of 00:12, 11 March 2013

Tip 6 Printable Monobook Previous Next

created 2001 · complexity basic · author Yegappan · version 6.0


% key

The % key can be used for the following:

  • 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.