FANDOM


 
(Made code work with the user's leader key, whichever key that is.)
 
(31 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{review}}
+
{{TipImported
{{Tip
 
 
|id=1005
 
|id=1005
|title=escape and unescape HTML entities
+
|previous=1004
|created=September 29, 2005 1:49
+
|next=1007
  +
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Jos van den Oever
 
|author=Jos van den Oever
|version=5.7
+
|version=6.0
 
|rating=6/5
 
|rating=6/5
|text=
+
|category1=HTML
if you add the following code to your .vimrc, you can escape your HTML entities (<, >, &) -> (<, >, &) with one shortcut key.
+
|category2=
 
 
 
:function HtmlEscape()
 
 
silent s/&/\&/eg
 
 
silent s/</\</eg
 
 
silent s/>/\>/eg
 
 
:endfunction
 
 
 
 
:function HtmlUnEscape()
 
 
silent s/</</eg
 
 
silent s/>/>/eg
 
 
silent s/&/\&/eg
 
 
:endfunction
 
 
 
 
vmap <silent> <c-h> :call HtmlEscape()<CR>
 
 
vmap <silent> <c-u> :call HtmlUnEscape()<CR>
 
 
 
 
}}
 
}}
  +
There are several ways to deal with HTML entities so that text can be edited, for example, while it contains a simple ampersand (<code>&</code>) rather than its HTML entity (<code>&amp;amp;</code>).
   
== Comments ==
+
==Simple search and replace==
:function HtmlEscape()
+
This code allows you to easily escape or unescape HTML entities: Change (<code><</code>, <code>></code>, <code>&</code>) to (<code>&amp;lt;</code>, <code>&amp;gt;</code>, <code>&amp;amp;</code>), or the reverse.
silent s/&amp;/\&amp;amp;/eg
 
silent s/&lt;/\&amp;lt;/eg
 
silent s/&gt;/\&amp;gt;/eg
 
:endfunction
 
   
:function HtmlUnEscape()
+
This does not escape all characters that should be escaped—just the most common.
silent s/&amp;lt;/&lt;/eg
+
<pre>
silent s/&amp;gt;/&gt;/eg
+
" Escape/unescape & < > HTML entities in range (default current line).
silent s/&amp;amp;/\&amp;/eg
+
function! HtmlEntities(line1, line2, action)
:endfunction
+
let search = @/
  +
let range = 'silent ' . a:line1 . ',' . a:line2
  +
if a:action == 0 " must convert &amp;amp; last
  +
execute range . 'sno/&amp;lt;/</eg'
  +
execute range . 'sno/&amp;gt;/>/eg'
  +
execute range . 'sno/&amp;amp;/&/eg'
  +
else " must convert & first
  +
execute range . 'sno/&/&amp;amp;/eg'
  +
execute range . 'sno/</&amp;lt;/eg'
  +
execute range . 'sno/>/&amp;gt;/eg'
  +
endif
  +
nohl
  +
let @/ = search
  +
endfunction
  +
command! -range -nargs=1 Entities call HtmlEntities(<line1>, <line2>, <args>)
  +
noremap <silent> <Leader>h :Entities 0<CR>
  +
noremap <silent> <Leader>H :Entities 1<CR>
  +
</pre>
   
vmap &lt;silent&gt; &lt;c-h&gt; :call HtmlEscape()&lt;CR&gt;
+
If you add the above code to your [[vimrc]], you can HTML escape the current line by typing <code>\H</code>, and unescape by typing <code>\h</code> (assuming the default backslash leader key). The same keys can be used to operate on all lines in a visually selected area, for example, select several lines then type <code>\h</code> to unescape them.
vmap &lt;silent&gt; &lt;c-u&gt; :call HtmlUnEscape()&lt;CR&gt;
 
   
  +
In addition, a user command is defined. It defaults to operating on the current line, but accepts a range. The argument is <code>0</code> to unescape, or <code>1</code> to escape, for example:
  +
<pre>
  +
" Unescape lines 10 to 20 inclusive.
  +
:10,20Entities 0
   
a--AT--b.cd
+
" Escape all lines.
, September 29, 2005 1:50
+
:%Entities 1
----
+
</pre>
The second version of the code is the correct one. If you add this code to your .vimrc, you can now escape HTML with &lt;ctrl&gt;-h and unescape the selected range with &lt;ctrl&gt;-u.
 
   
Good luck, Jos
+
==Automagic escaping==
  +
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.
   
  +
The script is for <code>&nnn</code> style encoding, not the HTML entities.
   
a--AT--b.cd
+
==Perl HTML::Entities==
, September 29, 2005 1:53
+
''Note: Vim needs to compiled with the "perl" feature enabled for this to work.''
----
 
Tooting my own horn: here's a 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: [/scripts/script.php?script_id=909 vimscript &#35;909].
 
   
I did this originally for java unicodes, but there is also a setting for html codes
+
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].
  +
<pre>
  +
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
   
Rog
+
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
   
'''Anonymous'''
+
nnoremap <Leader>h :call HTMLEncode()<CR>
, September 30, 2005 4:05
+
nnoremap <Leader>H :call HTMLDecode()<CR>
----
+
</pre>
sorry forgot to mention, that script is for &amp;nnn style encoding, not the html entities.
 
   
Rog
+
To convert a line, put the cursor in the line and type <code>\h</code> or <code>\H</code>.
   
'''Anonymous'''
+
==Ruby HTMLEncode==
, September 30, 2005 4:08
+
''Note: Vim needs to be compiled with the "ruby" feature enabled for this to work.''
----
 
I do it like this:
 
   
function! HTMLEncode()
+
The following is a simpler alternative using Ruby.
perl &lt;&lt; EOF
+
<pre>
use HTML::Entities;
+
function! HTMLEncode()
--AT--pos = $curwin-&gt;Cursor();
+
ruby << EOF
$line = $curbuf-&gt;Get($pos[0]);
+
@str=VIM::Buffer.current.line
$encvalue = encode_entities($line);
+
VIM::Buffer.current.line=@str.unpack("U*").collect {|s| (s > 127 ? "&##{s};" : s.chr) }.join("")
$curbuf-&gt;Set($pos[0],$encvalue)
+
EOF
EOF
+
endfunction
endfunction
 
   
function! HTMLDecode()
+
nnoremap <Leader>h :call HTMLEncode()<CR>
perl &lt;&lt; EOF
+
</pre>
use HTML::Entities;
 
--AT--pos = $curwin-&gt;Cursor();
 
$line = $curbuf-&gt;Get($pos[0]);
 
$encvalue = decode_entities($line);
 
$curbuf-&gt;Set($pos[0],$encvalue)
 
EOF
 
endfunction
 
   
This is quite extensible, for example LDAP BASE64 encoding:
+
==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 <code>,r</code> will check all the text and replace all three Norwegian special characters with entities. This can easily be applied to other languages.
  +
<pre>
  +
" Replace all Norwegian special characters with entities.
  +
nnoremap <silent> ,r :call ReplaceNorChar()<CR>
  +
function! ReplaceNorChar()
  +
silent %s/Æ/\&amp;AElig;/eg
  +
silent %s/Ø/\&amp;Oslash;/eg
  +
silent %s/Å/\&amp;Aring;/eg
  +
silent %s/æ/\&amp;aelig;/eg
  +
silent %s/ø/\&amp;oslash;/eg
  +
silent %s/å/\&amp;aring;/eg
  +
endfunction
  +
</pre>
   
function! LdapEncode64()
+
Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim.
perl &lt;&lt; EOF
 
use MIME::Base64;
 
--AT--pos = $curwin-&gt;Cursor();
 
$line = $curbuf-&gt;Get($pos[0]);
 
($key) = ( $line =~ m/^([\w;_\-]+):/);
 
($value) = ( $line =~ m/^[\w;_\-]+:\s*(.*)/);
 
$encvalue = encode_base64($value);
 
$encvalue =~ s/[\r|\n]//g;
 
$curbuf-&gt;Set($pos[0],$key.":: ".$encvalue)
 
EOF
 
endfunction
 
   
function! LdapDecode64()
+
==See also==
perl &lt;&lt; EOF
+
*[http://www.html-entities.org/ Quick reference and decode/encode tool]
use MIME::Base64;
+
*[http://puzzlersworld.com/misc/html_escape_tool.html Html escape tool]
--AT--pos = $curwin-&gt;Cursor();
 
$line = $curbuf-&gt;Get($pos[0]);
 
($key) = ( $line =~ m/^([\w;_\-]+)::/);
 
($value) = ( $line =~ m/^[\w;_\-]+::\s*(.*)/);
 
$decvalue = decode_base64($value);
 
$devvalue =~ s/[\r|\n]//g;
 
$curbuf-&gt;Set($pos[0],$key.": ".$decvalue)
 
EOF
 
endfunction
 
   
You can map them like this:
+
==Comments==
+
Can check it with:
map &lt;Leader&gt;b :call LdapEncode64()&lt;CR&gt;
+
<pre>
map &lt;Leader&gt;B :call LdapDecode64()&lt;CR&gt;
+
.! php -r "echo htmlentities('<cword>');"
map &lt;Leader&gt;h :call HTMLEncode()&lt;CR&gt;
+
</pre>
map &lt;Leader&gt;H :call HTMLDecode()&lt;CR&gt;
 
 
goto the line and do \h or \H to check it out,
 
 
tim dot esselens at gmail dot com
 
, September 30, 2005 8:20
 
----
 
This is a very good utility tip. I come across many xml escaped strings. This is the tool i was looking for. I extended this tool to change " to " character in my vimrc. A very handy and neet tool.
 
   
msrinirao--AT--gmail.com
 
, October 7, 2005 7:12
 
 
----
 
----
<!-- parsed by vimtips.py in 0.767325 seconds-->
+
<pre>
  +
command Entities :call Entities()
  +
function Entities()
  +
silent s/À/\&amp;Agrave;/eg
  +
silent s/Á/\&amp;Aacute;/eg
  +
silent s/Â/\&amp;Acirc;/eg
  +
silent s/Ã/\&amp;Atilde;/eg
  +
silent s/Ä/\&amp;Auml;/eg
  +
silent s/Å/\&amp;Aring;/eg
  +
silent s/Æ/\&amp;AElig;/eg
  +
silent s/Ç/\&amp;Ccedil;/eg
  +
silent s/È/\&amp;Egrave;/eg
  +
silent s/É/\&amp;Eacute;/eg
  +
silent s/Ê/\&amp;Ecirc;/eg
  +
silent s/Ë/\&amp;Euml;/eg
  +
silent s/Ì/\&amp;Igrave;/eg
  +
silent s/Í/\&amp;Iacute;/eg
  +
silent s/Î/\&amp;Icirc;/eg
  +
silent s/Ï/\&amp;Iuml;/eg
  +
silent s/Ð/\&amp;ETH;/eg
  +
silent s/Ñ/\&amp;Ntilde;/eg
  +
silent s/Ò/\&amp;Ograve;/eg
  +
silent s/Ó/\&amp;Oacute;/eg
  +
silent s/Ô/\&amp;Ocirc;/eg
  +
silent s/Õ/\&amp;Otilde;/eg
  +
silent s/Ö/\&amp;Ouml;/eg
  +
silent s/Ø/\&amp;Oslash;/eg
  +
silent s/Ù/\&amp;Ugrave;/eg
  +
silent s/Ú/\&amp;Uacute;/eg
  +
silent s/Û/\&amp;Ucirc;/eg
  +
silent s/Ü/\&amp;Uuml;/eg
  +
silent s/Ý/\&amp;Yacute;/eg
  +
silent s/Þ/\&amp;THORN;/eg
  +
silent s/ß/\&amp;szlig;/eg
  +
silent s/à/\&amp;agrave;/eg
  +
silent s/á/\&amp;aacute;/eg
  +
silent s/â/\&amp;acirc;/eg
  +
silent s/ã/\&amp;atilde;/eg
  +
silent s/ä/\&amp;auml;/eg
  +
silent s/å/\&amp;aring;/eg
  +
silent s/æ/\&amp;aelig;/eg
  +
silent s/ç/\&amp;ccedil;/eg
  +
silent s/è/\&amp;egrave;/eg
  +
silent s/é/\&amp;eacute;/eg
  +
silent s/ê/\&amp;ecirc;/eg
  +
silent s/ë/\&amp;euml;/eg
  +
silent s/ì/\&amp;igrave;/eg
  +
silent s/í/\&amp;iacute;/eg
  +
silent s/î/\&amp;icirc;/eg
  +
silent s/ï/\&amp;iuml;/eg
  +
silent s/ð/\&amp;eth;/eg
  +
silent s/ñ/\&amp;ntilde;/eg
  +
silent s/ò/\&amp;ograve;/eg
  +
silent s/ó/\&amp;oacute;/eg
  +
silent s/ô/\&amp;ocirc;/eg
  +
silent s/õ/\&amp;otilde;/eg
  +
silent s/ö/\&amp;ouml;/eg
  +
silent s/ø/\&amp;oslash;/eg
  +
silent s/ù/\&amp;ugrave;/eg
  +
silent s/ú/\&amp;uacute;/eg
  +
silent s/û/\&amp;ucirc;/eg
  +
silent s/ü/\&amp;uuml;/eg
  +
silent s/ý/\&amp;yacute;/eg
  +
silent s/þ/\&amp;thorn;/eg
  +
silent s/ÿ/\&amp;yuml;/eg
  +
endfunction
  +
</pre>

Latest revision as of 09:14, April 13, 2017

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 replaceEdit

This code allows you to easily escape or unescape HTML entities: Change (<, >, &) to (&lt;, &gt;, &amp;), or the reverse.

This does not escape all characters that should be escaped—just the most common.

" Escape/unescape & < > HTML entities in range (default current line).
function! HtmlEntities(line1, line2, action)
  let search = @/
  let range = 'silent ' . a:line1 . ',' . a:line2
  if a:action == 0  " must convert &amp; last
    execute range . 'sno/&lt;/</eg'
    execute range . 'sno/&gt;/>/eg'
    execute range . 'sno/&amp;/&/eg'
  else              " must convert & first
    execute range . 'sno/&/&amp;/eg'
    execute range . 'sno/</&lt;/eg'
    execute range . 'sno/>/&gt;/eg'
  endif
  nohl
  let @/ = search
endfunction
command! -range -nargs=1 Entities call HtmlEntities(<line1>, <line2>, <args>)
noremap <silent> <Leader>h :Entities 0<CR>
noremap <silent> <Leader>H :Entities 1<CR>

If you add the above code to your vimrc, you can HTML escape the current line by typing \H, and unescape by typing \h (assuming the default backslash leader key). The same keys can be used to operate on all lines in a visually selected area, for example, select several lines then type \h to unescape them.

In addition, a user command is defined. It defaults to operating on the current line, but accepts a range. The argument is 0 to unescape, or 1 to escape, for example:

" Unescape lines 10 to 20 inclusive.
:10,20Entities 0

" Escape all lines.
:%Entities 1

Automagic escapingEdit

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::EntitiesEdit

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 HTMLEncodeEdit

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-entitiesEdit

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.

See alsoEdit

CommentsEdit

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.