FANDOM


Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

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


Community content is available under CC-BY-SA unless otherwise noted.