Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Motion to target the matching brace/paren in Vim?

Tags:

vim

macvim

So, Vim, like most programming-minded text editors, highlights a matching "scope" character (such as ( and ), < and >, and { and }).

I'm wondering, is there a motion to reach that character from the other? Like, with the caret on a <, to move to the (highlighted) matching >?

I know [action]t> or [action]f> would accomplish something similar. That's not what I'm asking.

like image 810
ele Avatar asked Aug 28 '12 19:08

ele


1 Answers

The motion is: %.

Used alone, this jumps between open/close pairs based on the value of the matchpairs option.

But angle brackets - <> - aren't included by default. They may be set based on the filetype. The HTML filetype plugin sets it, so if you're opening HTML files you'll be able to use % to jump between matching angle brackets. But not for e.g. C++/Java.

To add angle brackets if they aren't jumped when using %, use:

:set matchpairs+=<:>

Now using % on angle brackets should jump to the matching bracket.

See :help 'matchpairs' and :help various-motions - % is the first mentioned - for more details.

like image 54
pb2q Avatar answered Sep 26 '22 01:09

pb2q