How do I make % jump between custom character pairs like < and >?
Answer
:set matchpairs+=<:>
Explanation
By default, % jumps between matching (), [], and {} pairs. Adding entries to matchpairs extends this to any pair of characters you choose, which is useful when working with languages that use <> for generics, template arguments, or HTML-like syntax.
How it works
matchpairsis a comma-separated list ofopen:closecharacter pairs- The
+=operator appends to the existing value instead of replacing it - After
:set matchpairs+=<:>, pressing%on a<jumps to the matching>and vice versa - This is purely character-based — it does not understand HTML tag names (for that, use
matchit)
Example
In a C++ template:
std::vector<int>
With the cursor on <, pressing % jumps to >. Without the custom pair, % would not recognize these.
Add multiple pairs in one command:
:set matchpairs+=<:>,|:|
Tips
- Put this in your
vimrc(without the colon) for persistence:set matchpairs+=<:> - To view the current value:
:set matchpairs? - The
matchitplugin (bundled with Vim/Neovim as an optional package) extends%further to match language keywords likeif/end, HTML tags, etc. - Enable matchit with:
:packadd matchit(Neovim) orruntime macros/matchit.vim(Vim)