No edit summary |
Luc Hermitte (talk | contribs) (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/& |
+ | silent s/</\&lt;/eg |
⚫ | |||
− | |||
⚫ | |||
− | |||
− | silent s/>/\>/eg |
||
− | |||
:endfunction |
:endfunction |
||
− | |||
− | |||
:function HtmlUnEscape() |
:function HtmlUnEscape() |
||
⚫ | |||
− | |||
− | silent s/& |
+ | silent s/&gt;/>/eg |
⚫ | |||
− | |||
− | silent s/>/>/eg |
||
− | |||
− | silent s/&/\&/eg |
||
− | |||
:endfunction |
:endfunction |
||
− | |||
− | |||
vmap <silent> <c-h> :call HtmlEscape()<CR> |
vmap <silent> <c-h> :call HtmlEscape()<CR> |
||
− | |||
vmap <silent> <c-u> :call HtmlUnEscape()<CR> |
vmap <silent> <c-u> :call HtmlUnEscape()<CR> |
||
+ | </pre> |
||
⚫ | |||
− | |||
}} |
}} |
||
== Comments == |
== Comments == |
||
⚫ | |||
− | :function HtmlEscape() |
||
⚫ | |||
− | silent s/</\&lt;/eg |
||
⚫ | |||
− | :endfunction |
||
− | |||
− | :function HtmlUnEscape() |
||
− | silent s/&lt;/</eg |
||
− | silent s/&gt;/>/eg |
||
⚫ | |||
− | :endfunction |
||
− | |||
− | vmap <silent> <c-h> :call HtmlEscape()<CR> |
||
− | vmap <silent> <c-u> :call HtmlUnEscape()<CR> |
||
− | |||
− | |||
− | a--AT--b.cd |
||
− | , September 29, 2005 1:50 |
||
− | ---- |
||
⚫ | |||
− | |||
− | Good luck, Jos |
||
− | |||
− | |||
− | a--AT--b.cd |
||
− | , September 29, 2005 1:53 |
||
− | ---- |
||
⚫ | |||
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 << EOF |
perl << 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 << EOF |
perl << EOF |
||
Line 137: | Line 97: | ||
EOF |
EOF |
||
endfunction |
endfunction |
||
+ | </pre> |
||
− | |||
You can map them like this: |
You can map them like this: |
||
+ | <pre> |
||
− | |||
map <Leader>b :call LdapEncode64()<CR> |
map <Leader>b :call LdapEncode64()<CR> |
||
map <Leader>B :call LdapDecode64()<CR> |
map <Leader>B :call LdapDecode64()<CR> |
||
map <Leader>h :call HTMLEncode()<CR> |
map <Leader>h :call HTMLEncode()<CR> |
||
map <Leader>H :call HTMLDecode()<CR> |
map <Leader>H :call HTMLDecode()<CR> |
||
+ | </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
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/&/\&/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>
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