vimtricks.wiki Concise Vim tricks, one at a time.

How do I make Vim highlight matching brackets when typing?

Answer

:set showmatch

Explanation

How it works

The :set showmatch option makes Vim briefly jump the cursor to the matching opening bracket when you type a closing bracket. This gives you visual confirmation that your brackets are properly matched as you code.

When you type ), ], or } in insert mode, the cursor briefly flashes to the matching (, [, or { and then returns to your typing position. This helps catch mismatched brackets immediately.

Related options:

  • :set showmatch - Enable the matching bracket highlight
  • :set matchtime=3 - Control how long the cursor stays on the match (in tenths of a second, default is 5 which is 0.5 seconds)
  • :set noshowmatch - Disable the feature

The matchtime option only affects the duration of the visual feedback. Setting it lower (like 2 or 3) gives a quicker flash that is less distracting during fast typing.

Note that this only works in insert mode when typing closing brackets. In normal mode, you can use % to jump between matching brackets at any time.

Vim also has the built-in matchparen plugin (loaded by default) which highlights the matching bracket pair when your cursor is on either one of them in normal mode. This is separate from showmatch.

Example

Add these lines to your ~/.vimrc:

set showmatch
set matchtime=2

Now when editing code:

function greet(name) {
    console.log("Hello, " + name);
}

As you type the closing ) after name, the cursor briefly jumps to the opening ( after greet, confirming the match. When you type the closing }, the cursor flashes to the opening {.

This is especially helpful in deeply nested code where it can be hard to visually track which bracket closes which block.

Next

How do you yank a single word into a named register?