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

How do I enable the built-in matchit plugin to make % jump between matching HTML tags and keywords?

Answer

:packadd! matchit

Explanation

Vim's % command jumps between matching brackets by default. The bundled matchit plugin extends this to also jump between matching HTML/XML tags and language keywords like if/else/end, do/done, and def/end. It ships with Vim but is not loaded automatically.

How it works

:packadd! matchit loads the plugin from Vim's optional package directory ($VIMRUNTIME/pack/dist/opt/matchit). The ! means the plugin is loaded immediately even if called from a startup file before VimEnter fires. For older Vim versions without the package system, use:

runtime macros/matchit.vim

Add either line to your ~/.vimrc or ~/.config/nvim/init.vim to enable it permanently.

Example

In an HTML file with the cursor on <div>:

<div>                ← cursor here, press %
  <p>Hello</p>
</div>               ← cursor jumps here

In a Vim script or Ruby file, pressing % on if jumps to elseif, then else, then endif.

Tips

  • Works across many filetypes: HTML, XML, Ruby, Vim script, shell scripts, and more
  • Respects filetype-specific match patterns defined in ftplugin files
  • Use :help matchit to see the full documentation after loading
  • Neovim users: matchit is available the same way and Neovim's bundled version supports Treesitter-aware matching in some cases

Next

How do I get just the filename without its path or extension to use in a command?