FANDOM


(Minor tweaks)
(Tweaks.)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
There are several ways to deal with HTML entities.
+
There are several ways to deal with HTML entities so that text can be edited, for example, while it contains a simple ampersand (<tt>&</tt>) rather than its HTML entity (<tt>&amp;amp;</tt>).
   
==Simple search & replace==
+
==Simple search and replace==
This code allows you to escape your HTML entities with one shortcut key: Change (<tt><, >, &</tt>) to (<tt>&amp;lt;, &amp;gt;, &amp;amp;</tt>), or the reverse.
+
This code allows you to escape your HTML entities with one shortcut key: Change (<tt><</tt>, <tt>></tt>, <tt>&</tt>) to (<tt>&amp;lt;</tt>, <tt>&amp;gt;</tt>, <tt>&amp;amp;</tt>), or the reverse.
   
Note that this does not escape all characters that should be escaped, just the most common.
+
This does not escape all characters that should be escaped—just the most common.
 
<pre>
 
<pre>
 
function HtmlEscape()
 
function HtmlEscape()
Line 30: Line 30:
 
endfunction
 
endfunction
   
map <silent> <c-h> :call HtmlEscape()<CR>
+
nnoremap <Leader>h :call HtmlEscape()<CR>
map <silent> <c-u> :call HtmlUnEscape()<CR>
+
nnoremap <Leader>H :call HtmlUnEscape()<CR>
 
</pre>
 
</pre>
   
If you add this code to your vimrc, you can escape visually-selected HTML with ctrl-h, and unescape with ctrl-u.
+
If you add this code to your [[vimrc]], you can escape visually-selected HTML by typing <tt>\h</tt>, and unescape by typing <tt>\H</tt> (assuming the default backslash leader key).
   
 
==Automagic escaping==
 
==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|id=909}}.
+
A script is available ({{script|id=909|text=unicodeswitch}}) that automagically converts entities when files are read and written, so you can view the characters, and write the codes, or vice versa. It was originally written for Java unicodes, but there is also a setting for HTML codes.
   
Originally written for Java unicodes, but there is also a setting for html codes.
+
The script is for <tt>&nnn</tt> style encoding, not the HTML entities.
   
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.''
   
==perl HTML::Entities==
+
A slightly more complex solution that escapes all characters uses Perl. You need [http://www.perl.org/ Perl] and [http://search.cpan.org/dist/HTML-Parser/ HTML-Parser].
''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 [http://www.perl.org/ perl] and [http://search.cpan.org/dist/HTML-Parser/ HTML-Parser]
 
 
<pre>
 
<pre>
 
function! HTMLEncode()
 
function! HTMLEncode()
Line 65: Line 65:
 
endfunction
 
endfunction
   
map <Leader>h :call HTMLEncode()<CR>
+
nnoremap <Leader>h :call HTMLEncode()<CR>
map <Leader>H :call HTMLDecode()<CR>
+
nnoremap <Leader>H :call HTMLDecode()<CR>
 
</pre>
 
</pre>
   
Go to the line and do <tt>\h</tt> or <tt>\H</tt> to check it out.
+
To convert a line, put the cursor in the line and type <tt>\h</tt> or <tt>\H</tt>.
   
==ruby version of HTMLEncode()==
+
==Ruby HTMLEncode==
''Note: Vim needs to be compiled with the "ruby" feature enabled for this to work''
+
''Note: Vim needs to be compiled with the "ruby" feature enabled for this to work.''
   
The following is a simpler ruby solution to the perl version of HTMLEncode above.
+
The following is a simpler alternative using Ruby.
 
<pre>
 
<pre>
 
function! HTMLEncode()
 
function! HTMLEncode()
Line 83: Line 83:
 
endfunction
 
endfunction
   
map <Leader>h :call HTMLEncode()<CR>
+
nnoremap <Leader>h :call HTMLEncode()<CR>
 
</pre>
 
</pre>
   
 
==Language specific HTML-entities==
 
==Language specific HTML-entities==
To change e.g. the Norwegian special characters there is no need to select text and not check all the text since it is never part of code-syntax (as far as I know). With the following, pressing &quot;,r&quot; from normal-mode will check all the text and replace all three Norwegian special chars with entities (and can easily be applied to other languages):
+
To change, for example, Norwegian special characters, there is no need to select text and not check all the text since it is never part of code-syntax. With the following, typing <tt>,r</tt> will check all the text and replace all three Norwegian special characters with entities. This can easily be applied to other languages.
 
 
<pre>
 
<pre>
" To replace all Norwegian special chars with entities.
+
" Replace all Norwegian special characters with entities.
nmap <silent> ,r :call ReplaceNorChar()<CR>
+
nnoremap <silent> ,r :call ReplaceNorChar()<CR>
 
function! ReplaceNorChar()
 
function! ReplaceNorChar()
silent! %s/Æ/\&amp;AElig;/eg
+
silent %s/Æ/\&amp;AElig;/eg
silent! %s/Ø/\&amp;Oslash;/eg
+
silent %s/Ø/\&amp;Oslash;/eg
silent! %s/Å/\&amp;Aring;/eg
+
silent %s/Å/\&amp;Aring;/eg
silent! %s/æ/\&amp;aelig;/eg
+
silent %s/æ/\&amp;aelig;/eg
silent! %s/ø/\&amp;oslash;/eg
+
silent %s/ø/\&amp;oslash;/eg
silent! %s/å/\&amp;aring;/eg
+
silent %s/å/\&amp;aring;/eg
 
endfunction
 
endfunction
 
</pre>
 
</pre>
   
 
Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim.
 
Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim.
 
   
 
==Comments==
 
==Comments==
Line 178: Line 177:
 
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>
 
: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)
 
 
very well done, thank you
 

Revision as of 08:34, July 4, 2012

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 so that text can be edited, for example, while it contains a simple ampersand (&) rather than its HTML entity (&amp;).

Simple search and replace

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

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

nnoremap <Leader>h :call HtmlEscape()<CR>
nnoremap <Leader>H :call HtmlUnEscape()<CR>

If you add this code to your vimrc, you can escape visually-selected HTML by typing \h, and unescape by typing \H (assuming the default backslash leader key).

Automagic escaping

A script is available (unicodeswitch) that automagically converts entities when files are read and written, so you can view the characters, and write the codes, or vice versa. It was 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 escapes all characters uses Perl. You 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

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

To convert a line, put the cursor in the line and type \h or \H.

Ruby HTMLEncode

Note: Vim needs to be compiled with the "ruby" feature enabled for this to work.

The following is a simpler alternative using Ruby.

function! HTMLEncode()
ruby << EOF
  @str=VIM::Buffer.current.line
  VIM::Buffer.current.line=@str.unpack("U*").collect {|s| (s > 127 ? "&##{s};" : s.chr) }.join("")
EOF
endfunction

nnoremap <Leader>h :call HTMLEncode()<CR>

Language specific HTML-entities

To change, for example, Norwegian special characters, there is no need to select text and not check all the text since it is never part of code-syntax. With the following, typing ,r will check all the text and replace all three Norwegian special characters with entities. This can easily be applied to other languages.

" Replace all Norwegian special characters with entities.
nnoremap <silent> ,r :call ReplaceNorChar()<CR>
function! ReplaceNorChar()
  silent %s/Æ/\&AElig;/eg
  silent %s/Ø/\&Oslash;/eg
  silent %s/Å/\&Aring;/eg
  silent %s/æ/\&aelig;/eg
  silent %s/ø/\&oslash;/eg
  silent %s/å/\&aring;/eg
endfunction

Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim.

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
Community content is available under CC-BY-SA unless otherwise noted.