Highlight matching bracket

3
(361)

In addition to jumping to the corresponding bracket or parenthesis with %, Vim has a little-known feature for showing corresponding parens when inserting.

Vim’s showmatch feature might be useful for those writing code with many nested parentheses, such as Lisp. With :setshowmatch enabled, Vim enables a special bracket highlighting while in insert mode. After typing a paren, brace, or bracket, the cursor will automatically jump to the matching bracket ever so briefly. Just long enough for you to visually confirm which parenthesis you are closing. This can reduce the need to even use the % command to bounce to the bracket manually.

You can change the time the cursor spends there before bouncing back with matchtime.

Here’s how I use showmatch:

:set showmatch matchtime=3

This feature is best demonstrated so watch the brief screencast here. Notice how the insert-mode cursor jumps to the opposite bracket, highlighting the opposite side.

If you’re working on a tiny screen, or on text with parens that span many lines, you may wish to have the screen scroll automatically when showmatch is enabled. Add this to your .vimrc:

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 

How useful was this tip?

Average rating 3 / 5. Vote count: 361

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts