Vim Tips Wiki
No edit summary
 
(Added to HTML category + code reformatted + comments taken into account)
Line 11: Line 11:
 
if you add the following code to your .vimrc, you can escape your HTML entities (<, >, &) -> (<, >, &) with one shortcut key.
 
if you add the following code to your .vimrc, you can escape your HTML entities (<, >, &) -> (<, >, &) with one shortcut key.
   
  +
<pre>
 
 
 
:function HtmlEscape()
 
:function HtmlEscape()
 
silent s/&amp;/\&amp;amp;/eg
 
silent s/&amp;/\&amp;/eg
+
silent s/&lt;/\&amp;lt;/eg
 
silent s/&gt;/\&amp;gt;/eg
 
silent s/&lt;/\&lt;/eg
 
 
silent s/&gt;/\&gt;/eg
 
 
 
:endfunction
 
:endfunction
 
 
   
 
:function HtmlUnEscape()
 
:function HtmlUnEscape()
 
silent s/&amp;lt;/&lt;/eg
 
silent s/&lt;/&lt;/eg
+
silent s/&amp;gt;/&gt;/eg
 
silent s/&amp;amp;/\&amp;/eg
 
silent s/&gt;/&gt;/eg
 
 
silent s/&amp;/\&amp;/eg
 
 
 
:endfunction
 
:endfunction
 
 
   
 
vmap &lt;silent&gt; &lt;c-h&gt; :call HtmlEscape()&lt;CR&gt;
 
vmap &lt;silent&gt; &lt;c-h&gt; :call HtmlEscape()&lt;CR&gt;
 
 
vmap &lt;silent&gt; &lt;c-u&gt; :call HtmlUnEscape()&lt;CR&gt;
 
vmap &lt;silent&gt; &lt;c-u&gt; :call HtmlUnEscape()&lt;CR&gt;
  +
</pre>
   
 
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.
 
 
}}
 
}}
   
 
== Comments ==
 
== Comments ==
 
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: {{script|id=909}}.
:function HtmlEscape()
 
silent s/&amp;/\&amp;amp;/eg
 
silent s/&lt;/\&amp;lt;/eg
 
silent s/&gt;/\&amp;gt;/eg
 
:endfunction
 
 
:function HtmlUnEscape()
 
silent s/&amp;lt;/&lt;/eg
 
silent s/&amp;gt;/&gt;/eg
 
silent s/&amp;amp;/\&amp;/eg
 
:endfunction
 
 
vmap &lt;silent&gt; &lt;c-h&gt; :call HtmlEscape()&lt;CR&gt;
 
vmap &lt;silent&gt; &lt;c-u&gt; :call HtmlUnEscape()&lt;CR&gt;
 
 
 
a--AT--b.cd
 
, September 29, 2005 1:50
 
----
 
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
 
 
 
a--AT--b.cd
 
, September 29, 2005 1:53
 
----
 
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
 
I did this originally for java unicodes, but there is also a setting for html codes
Line 89: Line 49:
 
----
 
----
 
I do it like this:
 
I do it like this:
  +
<pre>
 
 
function! HTMLEncode()
 
function! HTMLEncode()
 
perl &lt;&lt; EOF
 
perl &lt;&lt; EOF
Line 109: Line 69:
 
EOF
 
EOF
 
endfunction
 
endfunction
  +
</pre>
 
 
This is quite extensible, for example LDAP BASE64 encoding:
 
This is quite extensible, for example LDAP BASE64 encoding:
  +
<pre>
 
 
function! LdapEncode64()
 
function! LdapEncode64()
 
perl &lt;&lt; EOF
 
perl &lt;&lt; EOF
Line 137: Line 97:
 
EOF
 
EOF
 
endfunction
 
endfunction
  +
</pre>
 
 
You can map them like this:
 
You can map them like this:
  +
<pre>
 
 
map &lt;Leader&gt;b :call LdapEncode64()&lt;CR&gt;
 
map &lt;Leader&gt;b :call LdapEncode64()&lt;CR&gt;
 
map &lt;Leader&gt;B :call LdapDecode64()&lt;CR&gt;
 
map &lt;Leader&gt;B :call LdapDecode64()&lt;CR&gt;
 
map &lt;Leader&gt;h :call HTMLEncode()&lt;CR&gt;
 
map &lt;Leader&gt;h :call HTMLEncode()&lt;CR&gt;
 
map &lt;Leader&gt;H :call HTMLDecode()&lt;CR&gt;
 
map &lt;Leader&gt;H :call HTMLDecode()&lt;CR&gt;
  +
</pre>
 
 
goto the line and do \h or \H to check it out,
 
goto the line and do \h or \H to check it out,
   
Line 156: Line 116:
 
----
 
----
 
<!-- parsed by vimtips.py in 0.767325 seconds-->
 
<!-- parsed by vimtips.py in 0.767325 seconds-->
  +
  +
[[Category:HTML]]

Revision as of 18:36, 22 August 2007

Previous TipNext Tip

Tip: #1005 - HTML entities

Created: September 29, 2005 1:49 Complexity: basic Author: Jos van den Oever Version: 5.7 Karma: 6/5 Imported from: Tip#1005

if you add the following code to your .vimrc, you can escape your HTML entities (<, >, &) -> (<, >, &) with one shortcut key.

: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 

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

If you add this code to your .vimrc, you can now escape HTML with <ctrl>-h and unescape the selected range with <ctrl>-u.

Comments

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: script#909.

I did this originally for java unicodes, but there is also a setting for html codes

Rog

Anonymous , September 30, 2005 4:05


sorry forgot to mention, that script is for &nnn style encoding, not the html entities.

Rog

Anonymous , September 30, 2005 4:08


I do it like this:

function! HTMLEncode() 
perl << EOF 
 use HTML::Entities; 
 --AT--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; 
 --AT--pos = $curwin->Cursor(); 
 $line = $curbuf->Get($pos[0]); 
 $encvalue = decode_entities($line); 
 $curbuf->Set($pos[0],$encvalue) 
EOF 
endfunction 

This is quite extensible, for example LDAP BASE64 encoding:

function! LdapEncode64() 
perl << EOF 
 use MIME::Base64; 
 --AT--pos = $curwin->Cursor(); 
 $line = $curbuf->Get($pos[0]); 
 ($key) = ( $line =~ m/^([\w;_\-]+):/); 
 ($value) = ( $line =~ m/^[\w;_\-]+:\s*(.*)/); 
 $encvalue = encode_base64($value); 
 $encvalue =~ s/[\r|\n]//g; 
 $curbuf->Set($pos[0],$key.":: ".$encvalue) 
EOF 
endfunction 

function! LdapDecode64() 
perl << EOF 
 use MIME::Base64; 
 --AT--pos = $curwin->Cursor(); 
 $line = $curbuf->Get($pos[0]); 
 ($key) = ( $line =~ m/^([\w;_\-]+)::/); 
 ($value) = ( $line =~ m/^[\w;_\-]+::\s*(.*)/); 
 $decvalue = decode_base64($value); 
 $devvalue =~ s/[\r|\n]//g; 
 $curbuf->Set($pos[0],$key.": ".$decvalue) 
EOF 
endfunction 

You can map them like this:

map <Leader>b :call LdapEncode64()<CR> 
map <Leader>B :call LdapDecode64()<CR> 
map <Leader>h :call HTMLEncode()<CR> 
map <Leader>H :call HTMLDecode()<CR> 

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