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
matchpairsvalue:(:),{:},[:]— parentheses, curly braces, square brackets :set matchpairs+=<:>— adds<and>as a matching pair- Syntax:
open:closepairs 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 matchitin modern Vim) which extends%to work with keywords and HTML tags - Put filetype-specific
matchpairssettings in~/.vim/ftplugin/{filetype}.vimso they activate automatically - The
matchpairsoption only affects%— it does not affect text objects likevi<or highlight matching withshowmatch :set showmatch(orsm) flashes the matching bracket when you type a closing bracket — it also usesmatchpairs