Vim Tips Wiki
(→‎Simple search & replace: use & entity so we show the desired text)
Line 14: Line 14:
   
 
==Simple search & replace==
 
==Simple search & replace==
This code allows you to escape your HTML entities with one shortcut key: Change (<tt><, >, &</tt>) to (<tt>&lt;, &gt;, &amp;</tt>), or the reverse.
+
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.
   
 
Note that this does not escape all characters that should be escaped, just the most common.
 
Note that this does not escape all characters that should be escaped, just the most common.
Line 20: Line 20:
 
<pre>
 
<pre>
 
function HtmlEscape()
 
function HtmlEscape()
silent s/&/\&amp;/eg
+
silent s/&/\&amp;amp;/eg
silent s/</\&lt;/eg
+
silent s/</\&amp;lt;/eg
silent s/>/\&gt;/eg
+
silent s/>/\&amp;gt;/eg
 
endfunction
 
endfunction
   
 
function HtmlUnEscape()
 
function HtmlUnEscape()
silent s/&lt;/</eg
+
silent s/&amp;lt;/</eg
silent s/&gt;/>/eg
+
silent s/&amp;gt;/>/eg
silent s/&amp;/\&/eg
+
silent s/&amp;amp;/\&/eg
 
endfunction
 
endfunction
   

Revision as of 17:01, 7 June 2010

Tip 1005 Printable Monobook Previous Next

created September 29, 2005 · complexity basic · author Jos van den Oever · version 5.7


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>');"