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

How do I make the % key jump between custom bracket pairs like < and >?

Answer

:set matchpairs+=<:>

Explanation

By default, % jumps between matching (), {}, and [] pairs. The matchpairs option controls which delimiter pairs are recognised. Using += appends a new pair without removing the defaults, so you can add any ASCII delimiter pair you need.

How it works

  • matchpairs is a comma-separated list of open:close pairs
  • Default value: (:),{:},[:]
  • :set matchpairs+=<:> appends <:> to the list
  • After this, pressing % on < jumps to the matching > and vice versa

Each pair is written as open:close using any two distinct ASCII characters.

Example

With :set matchpairs+=<:> active, given:

<template v-for="item in items">

Placing the cursor on < and pressing % jumps to the closing > at the end of the tag.

Tips

  • Add multiple pairs at once: :set matchpairs+=<:>,«:»
  • For filetype-specific pairs, put the setting in ~/.vim/ftplugin/{filetype}.vim or use an autocmd:
    autocmd FileType html setlocal matchpairs+=<:>
    
  • The built-in matchit plugin extends % further to handle multi-character pairs like if/end in Ruby or do/end in shell scripts: :packadd matchit
  • To view your current pairs: :set matchpairs?

Next

How do I refer to the matched text in a Vim substitution replacement?