This is a card in Dave's Virtual Box of Cards.

Vim: A little shortcut to convert HTML entities

Page created: 2026-10-27

Back to Vim!

Tired of typing the HTML entities for <, >, and & in your hand-written HTML?

Here’s a stupid simple one-line shortcut for Vim:

vnoremap <leader>hh :s/&/\&amp;/g<cr>gv:s/</\&lt;/g<cr>gv:s/>/\&gt;/g<cr>:noh<cr>

To use:

Notes: The s/// substitution acts on the current selection in visual mode. I use gv to re-select the selection between substitutions. The noh abbreviation un-highlights the last replacement.

Also: The default leader key is \, but you can see if it’s been changed with :echo mapleader. ("Undefined variable" means it’s still the default.)