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/&/\&/g<cr>gv:s/</\</g<cr>gv:s/>/\>/g<cr>:noh<cr>
To use:
-
Select some text with visual mode
-
Type the leader key, then hit the
Hkey twice.
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.)