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

How do I add custom bracket pairs so that % jumps between them?

Answer

:set matchpairs+={char}:{char}

Explanation

The % key jumps between matching pairs of brackets, tags, and delimiters. The matchpairs option defines which pairs % recognizes. By adding to it, you can make % jump between any two characters — such as </> angle brackets, or custom language-specific delimiters.

How it works

  • Default matchpairs value: (:),{:},[:] — parentheses, curly braces, square brackets
  • :set matchpairs+=<:> — adds < and > as a matching pair
  • Syntax: open:close pairs separated by commas
  • :set matchpairs? shows the current value
  • Use :setlocal matchpairs+=<:> to apply only to the current buffer (recommended in ftplugin files)

Example

In an HTML or template file, add angle bracket matching:

:set matchpairs+=<:>

Now % on <div> jumps to > and back.

For a custom language with |...| delimiters:

:set matchpairs+=|:|

To reset to defaults:

:set matchpairs&

Tips

  • For complex language-aware matching (e.g., if/end, do/done), consider the matchit plugin (:packadd matchit in modern Vim) which extends % to work with keywords and HTML tags
  • Put filetype-specific matchpairs settings in ~/.vim/ftplugin/{filetype}.vim so they activate automatically
  • The matchpairs option only affects % — it does not affect text objects like vi< or highlight matching with showmatch
  • :set showmatch (or sm) flashes the matching bracket when you type a closing bracket — it also uses matchpairs

Next

How do I run a search and replace only within a visually selected region?