FANDOM


(format/fix new comment and ask question)
(hmmm, won't it need a backslash before each ampersand? was this tested?)
Line 84: Line 84:
 
command Entities :call Entities()
 
command Entities :call Entities()
 
function Entities()
 
function Entities()
silent s/À/À/eg
+
silent s/À/\À/eg
silent s/Á/Á/eg
+
silent s/Á/\Á/eg
silent s/Â/Â/eg
+
silent s/Â/\Â/eg
silent s/Ã/Ã/eg
+
silent s/Ã/\Ã/eg
silent s/Ä/Ä/eg
+
silent s/Ä/\Ä/eg
silent s/Å/Å/eg
+
silent s/Å/\Å/eg
silent s/Æ/Æ/eg
+
silent s/Æ/\Æ/eg
silent s/Ç/Ç/eg
+
silent s/Ç/\Ç/eg
silent s/È/È/eg
+
silent s/È/\È/eg
silent s/É/É/eg
+
silent s/É/\É/eg
silent s/Ê/Ê/eg
+
silent s/Ê/\Ê/eg
silent s/Ë/Ë/eg
+
silent s/Ë/\Ë/eg
silent s/Ì/Ì/eg
+
silent s/Ì/\Ì/eg
silent s/Í/Í/eg
+
silent s/Í/\Í/eg
silent s/Î/Î/eg
+
silent s/Î/\Î/eg
silent s/Ï/Ï/eg
+
silent s/Ï/\Ï/eg
silent s/Ð/Ð/eg
+
silent s/Ð/\Ð/eg
silent s/Ñ/Ñ/eg
+
silent s/Ñ/\Ñ/eg
silent s/Ò/Ò/eg
+
silent s/Ò/\Ò/eg
silent s/Ó/Ó/eg
+
silent s/Ó/\Ó/eg
silent s/Ô/Ô/eg
+
silent s/Ô/\Ô/eg
silent s/Õ/Õ/eg
+
silent s/Õ/\Õ/eg
silent s/Ö/Ö/eg
+
silent s/Ö/\Ö/eg
silent s/Ø/Ø/eg
+
silent s/Ø/\Ø/eg
silent s/Ù/Ù/eg
+
silent s/Ù/\Ù/eg
silent s/Ú/Ú/eg
+
silent s/Ú/\Ú/eg
silent s/Û/Û/eg
+
silent s/Û/\Û/eg
silent s/Ü/Ü/eg
+
silent s/Ü/\Ü/eg
silent s/Ý/Ý/eg
+
silent s/Ý/\Ý/eg
silent s/Þ/Þ/eg
+
silent s/Þ/\Þ/eg
silent s/ß/ß/eg
+
silent s/ß/\ß/eg
silent s/à/à/eg
+
silent s/à/\à/eg
silent s/á/á/eg
+
silent s/á/\á/eg
silent s/â/â/eg
+
silent s/â/\â/eg
silent s/ã/ã/eg
+
silent s/ã/\ã/eg
silent s/ä/ä/eg
+
silent s/ä/\ä/eg
silent s/å/å/eg
+
silent s/å/\å/eg
silent s/æ/æ/eg
+
silent s/æ/\æ/eg
silent s/ç/ç/eg
+
silent s/ç/\ç/eg
silent s/è/è/eg
+
silent s/è/\è/eg
silent s/é/é/eg
+
silent s/é/\é/eg
silent s/ê/ê/eg
+
silent s/ê/\ê/eg
silent s/ë/ë/eg
+
silent s/ë/\ë/eg
silent s/ì/ì/eg
+
silent s/ì/\ì/eg
silent s/í/í/eg
+
silent s/í/\í/eg
silent s/î/î/eg
+
silent s/î/\î/eg
silent s/ï/ï/eg
+
silent s/ï/\ï/eg
silent s/ð/ð/eg
+
silent s/ð/\ð/eg
silent s/ñ/ñ/eg
+
silent s/ñ/\ñ/eg
silent s/ò/ò/eg
+
silent s/ò/\ò/eg
silent s/ó/ó/eg
+
silent s/ó/\ó/eg
silent s/ô/ô/eg
+
silent s/ô/\ô/eg
silent s/õ/õ/eg
+
silent s/õ/\õ/eg
silent s/ö/ö/eg
+
silent s/ö/\ö/eg
silent s/ø/ø/eg
+
silent s/ø/\ø/eg
silent s/ù/ù/eg
+
silent s/ù/\ù/eg
silent s/ú/ú/eg
+
silent s/ú/\ú/eg
silent s/û/û/eg
+
silent s/û/\û/eg
silent s/ü/ü/eg
+
silent s/ü/\ü/eg
silent s/ý/ý/eg
+
silent s/ý/\ý/eg
silent s/þ/þ/eg
+
silent s/þ/\þ/eg
silent s/ÿ/ÿ/eg
+
silent s/ÿ/\ÿ/eg
 
endfunction
 
endfunction
 
</pre>
 
</pre>
 
<small>--Preceding [[Vim Tips Wiki:Quick reference|unsigned]] comment added by [[User:212.145.191.182|212.145.191.182]] 11:11, July 23, 2010</small>
 
<small>--Preceding [[Vim Tips Wiki:Quick reference|unsigned]] comment added by [[User:212.145.191.182|212.145.191.182]] 11:11, July 23, 2010</small>
:I formatted the above. What was the dot doing in <tt>s./old/new/eg</tt>? Is the dot supposed to be before the <tt>s</tt> (the current line)? If so, it is redundant because the default is the current line. I removed the dot from each command. [[User:JohnBeckett|JohnBeckett]] 12:04, July 23, 2010 (UTC)
+
:I formatted the above. What was the dot doing in <tt>s./old/new/eg</tt>? Is the dot supposed to be before the <tt>s</tt> (the current line)? If so, it is redundant because the default is the current line. I removed the dot from each command. [[User:JohnBeckett|JohnBeckett]] 12:07, July 23, 2010 (UTC)

Revision as of 12:07, July 23, 2010

Tip 1005 Printable Monobook Previous Next

created 2005 · complexity basic · author Jos van den Oever · version 6.0


There are several ways to deal with HTML entities.

Simple search & replace

This code allows you to escape your HTML entities with one shortcut key: Change (<, >, &) to (&lt;, &gt;, &amp;), or the reverse.

Note that this does not escape all characters that should be escaped, just the most common.

function HtmlEscape()
  silent s/&/\&amp;/eg
  silent s/</\&lt;/eg
  silent s/>/\&gt;/eg
endfunction

function HtmlUnEscape()
  silent s/&lt;/</eg
  silent s/&gt;/>/eg
  silent s/&amp;/\&/eg
endfunction

map <silent> <c-h> :call HtmlEscape()<CR>
map <silent> <c-u> :call HtmlUnEscape()<CR>

If you add this code to your vimrc, you can escape visually-selected HTML with ctrl-h, and unescape with ctrl-u.

Automagic escaping

There's also script that does this for you automagically when you read and write files, so you can view the characters, and write the codes, or vice versa: script#909.

Originally written for Java unicodes, but there is also a setting for html codes.

The script is for &nnn style encoding, not the html entities.

perl HTML::Entities

Note: Vim needs to compiled with the "perl" feature enabled for this to work

A slightly more complex solution that escape all characters is using perl, you will need perl and HTML-Parser

function! HTMLEncode()
perl << EOF
 use HTML::Entities;
 @pos = $curwin->Cursor();
 $line = $curbuf->Get($pos[0]);
 $encvalue = encode_entities($line);
 $curbuf->Set($pos[0],$encvalue)
EOF
endfunction

function! HTMLDecode()
perl << EOF
 use HTML::Entities;
 @pos = $curwin->Cursor();
 $line = $curbuf->Get($pos[0]);
 $encvalue = decode_entities($line);
 $curbuf->Set($pos[0],$encvalue)
EOF
endfunction

map <Leader>h :call HTMLEncode()<CR>
map <Leader>H :call HTMLDecode()<CR>

Go to the line and do \h or \H to check it out.

Comments

Can check it with:

.! php -r "echo htmlentities('<cword>');"

command Entities :call Entities()
function Entities()
  silent s/À/\&Agrave;/eg
  silent s/Á/\&Aacute;/eg
  silent s/Â/\&Acirc;/eg
  silent s/Ã/\&Atilde;/eg
  silent s/Ä/\&Auml;/eg
  silent s/Å/\&Aring;/eg
  silent s/Æ/\&AElig;/eg
  silent s/Ç/\&Ccedil;/eg
  silent s/È/\&Egrave;/eg
  silent s/É/\&Eacute;/eg
  silent s/Ê/\&Ecirc;/eg
  silent s/Ë/\&Euml;/eg
  silent s/Ì/\&Igrave;/eg
  silent s/Í/\&Iacute;/eg
  silent s/Î/\&Icirc;/eg
  silent s/Ï/\&Iuml;/eg
  silent s/Ð/\&ETH;/eg
  silent s/Ñ/\&Ntilde;/eg
  silent s/Ò/\&Ograve;/eg
  silent s/Ó/\&Oacute;/eg
  silent s/Ô/\&Ocirc;/eg
  silent s/Õ/\&Otilde;/eg
  silent s/Ö/\&Ouml;/eg
  silent s/Ø/\&Oslash;/eg
  silent s/Ù/\&Ugrave;/eg
  silent s/Ú/\&Uacute;/eg
  silent s/Û/\&Ucirc;/eg
  silent s/Ü/\&Uuml;/eg
  silent s/Ý/\&Yacute;/eg
  silent s/Þ/\&THORN;/eg
  silent s/ß/\&szlig;/eg
  silent s/à/\&agrave;/eg
  silent s/á/\&aacute;/eg
  silent s/â/\&acirc;/eg
  silent s/ã/\&atilde;/eg
  silent s/ä/\&auml;/eg
  silent s/å/\&aring;/eg
  silent s/æ/\&aelig;/eg
  silent s/ç/\&ccedil;/eg
  silent s/è/\&egrave;/eg
  silent s/é/\&eacute;/eg
  silent s/ê/\&ecirc;/eg
  silent s/ë/\&euml;/eg
  silent s/ì/\&igrave;/eg
  silent s/í/\&iacute;/eg
  silent s/î/\&icirc;/eg
  silent s/ï/\&iuml;/eg
  silent s/ð/\&eth;/eg
  silent s/ñ/\&ntilde;/eg
  silent s/ò/\&ograve;/eg
  silent s/ó/\&oacute;/eg
  silent s/ô/\&ocirc;/eg
  silent s/õ/\&otilde;/eg
  silent s/ö/\&ouml;/eg
  silent s/ø/\&oslash;/eg
  silent s/ù/\&ugrave;/eg
  silent s/ú/\&uacute;/eg
  silent s/û/\&ucirc;/eg
  silent s/ü/\&uuml;/eg
  silent s/ý/\&yacute;/eg
  silent s/þ/\&thorn;/eg
  silent s/ÿ/\&yuml;/eg
endfunction

--Preceding unsigned comment added by 212.145.191.182 11:11, July 23, 2010

I formatted the above. What was the dot doing in s./old/new/eg? Is the dot supposed to be before the s (the current line)? If so, it is redundant because the default is the current line. I removed the dot from each command. JohnBeckett 12:07, July 23, 2010 (UTC)
Community content is available under CC-BY-SA unless otherwise noted.