How do I use % to jump between angle brackets in HTML or XML files?
Answer
:set matchpairs+=<:>
Explanation
By default, the % command jumps between (), {}, and [] pairs. It does not recognize <> angle brackets, which means % won't jump between HTML/XML tags or template syntax. Adding <:> to the matchpairs option extends % to work with angle brackets too.
How it works
:set matchpairs+=<:>— adds angle brackets as a recognized pair for%- Place the cursor on a
<or>character and press%to jump to its match - This works in normal mode and can be combined with operators like
d%,c%, andy%
The matchpairs option takes a comma-separated list of open:close character pairs. The default value is (:),{:},[:].
Example
With the cursor on the opening < of an HTML tag:
<div class="container">
█ cursor here
</div>
Pressing % on < jumps to the matching > on the same tag. Pressing % again jumps back.
<div class="container█">
content
</div>
Tips
- Set this per filetype:
autocmd FileType html,xml,eruby setlocal matchpairs+=<:> - This only matches single
<and>characters, not full tag pairs — for full tag matching, use the built-inmatchitplugin with% - Useful for navigating C++ templates, Rust generics, and other languages that use angle brackets