Liomblr RSS

Archive

Sep
28th
Mon
permalink

[Vim] Trials and errors on EscapeXmlSpecialChars()

1. This is not what I want.
function EscapeXmlSpecialChars()
  s/&/\&/eg
  s/</\&lt;/eg
  s/>/\&gt;/eg
  s/'/\&apos;/eg
  s/"/\&quot;/eg
endfunction
2. This does not work properly.
vnoremap <buffer> <LocalLeader>e :call EscapeXmlSpecialChars()<CR>

function EscapeXmlSpecialChars()
  s/\%V&/\&amp;/eg
  s/\%V</\&lt;/eg
  s/\%V>/\&gt;/eg
  s/\%V'/\&apos;/eg
  s/\%V"/\&quot;/eg
endfunction
3. This looks good :)
vnoremap <buffer> <LocalLeader>e "xx:call <SID>EscapeXmlSpecialChars()<CR>"xP

function s:EscapeXmlSpecialChars()
  let @x = substitute(@x, '&', '\&amp;', 'g')
  let @x = substitute(@x, '<', '\&lt;', 'g')
  let @x = substitute(@x, '>', '\&gt;', 'g')
  let @x = substitute(@x, "'", '\&apos;', 'g')
  let @x = substitute(@x, '"', '\&quot;', 'g')
endfunction