This Vim macro converts <, >, ", and & into <, >, ", and & respectively. You can use this macro in both normal and visual modes. Its main use so far has been to convert scripts and code for posting to this journal.
" ------------------------------------------------------------------
" Convert text to HTML by escaping <, >, ", and &.
function! HtmlizeText()
let line = getline(".")
let line = substitute(line, '&', '\&', 'g')
let line = substitute(line, '<', '\<', 'g')
let line = substitute(line, '>', '\>', 'g')
let line = substitute(line, '"', '\"', 'g')
call setline(".", line)
endfunction
nmap ,h :call HtmlizeText()<cr>
vmap ,h :call HtmlizeText()<cr>