(Made code work with the user's leader key, whichever key that is.) Tags: Visual edit apiedit |
|||
(12 intermediate revisions by 8 users not shown) | |||
Line 11: | Line 11: | ||
|category2= |
|category2= |
||
}} |
}} |
||
− | There are several ways to deal with HTML entities. |
+ | 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;</code>). |
− | ==Simple search |
+ | ==Simple search and replace== |
− | This code allows you to escape |
+ | This code allows you to easily escape or unescape HTML entities: Change (<code><</code>, <code>></code>, <code>&</code>) to (<code>&lt;</code>, <code>&gt;</code>, <code>&amp;</code>), or the reverse. |
− | + | This does not escape all characters that should be escaped—just the most common. |
|
<pre> |
<pre> |
||
+ | " Escape/unescape & < > HTML entities in range (default current line). |
||
− | function HtmlEscape() |
||
+ | function! HtmlEntities(line1, line2, action) |
||
− | silent s/&/\&amp;/eg |
||
+ | let search = @/ |
||
− | silent s/</\&lt;/eg |
||
+ | let range = 'silent ' . a:line1 . ',' . a:line2 |
||
− | silent s/>/\&gt;/eg |
||
+ | 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 |
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> |
||
+ | 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. |
||
− | function HtmlUnEscape() |
||
− | silent s/&lt;/</eg |
||
− | silent s/&gt;/>/eg |
||
− | silent s/&amp;/\&/eg |
||
− | endfunction |
||
+ | 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: |
||
− | map <silent> <c-h> :call HtmlEscape()<CR> |
||
+ | <pre> |
||
− | map <silent> <c-u> :call HtmlUnEscape()<CR> |
||
+ | " Unescape lines 10 to 20 inclusive. |
||
+ | :10,20Entities 0 |
||
+ | |||
+ | " Escape all lines. |
||
+ | :%Entities 1 |
||
</pre> |
</pre> |
||
− | |||
− | If you add this code to your vimrc, you can escape visually-selected HTML with ctrl-h, and unescape with ctrl-u. |
||
==Automagic escaping== |
==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. |
|
− | |||
− | Originally written for Java unicodes, but there is also a setting for html codes. |
||
− | The script is for &nnn style encoding, not the |
+ | The script is for <code>&nnn</code> style encoding, not the HTML entities. |
− | == |
+ | ==Perl HTML::Entities== |
− | ''Note: Vim needs to compiled with the "perl" feature enabled for this to work'' |
+ | ''Note: Vim needs to compiled with the "perl" feature enabled for this to work.'' |
− | A slightly more complex solution that |
+ | 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> |
<pre> |
||
function! HTMLEncode() |
function! HTMLEncode() |
||
Line 68: | Line 80: | ||
endfunction |
endfunction |
||
− | + | nnoremap <Leader>h :call HTMLEncode()<CR> |
|
− | + | nnoremap <Leader>H :call HTMLDecode()<CR> |
|
</pre> |
</pre> |
||
− | + | To convert a line, put the cursor in the line and type <code>\h</code> or <code>\H</code>. |
|
− | == |
+ | ==Ruby HTMLEncode== |
− | ''Note: Vim needs to be compiled with the "ruby" feature enabled for this to work'' |
+ | ''Note: Vim needs to be compiled with the "ruby" feature enabled for this to work.'' |
− | The following is a simpler |
+ | The following is a simpler alternative using Ruby. |
<pre> |
<pre> |
||
− | function! HTMLEncode() |
+ | function! HTMLEncode() |
+ | ruby << EOF |
||
− | ruby << EOF |
||
− | @str=VIM::Buffer.current.line |
+ | @str=VIM::Buffer.current.line |
− | VIM::Buffer.current.line=@str.unpack("U*").collect {|s| (s > 127 ? "&##{s};" : s.chr) }.join("") |
+ | VIM::Buffer.current.line=@str.unpack("U*").collect {|s| (s > 127 ? "&##{s};" : s.chr) }.join("") |
− | EOF |
+ | EOF |
endfunction |
endfunction |
||
− | + | nnoremap <Leader>h :call HTMLEncode()<CR> |
|
</pre> |
</pre> |
||
==Language specific HTML-entities== |
==Language specific HTML-entities== |
||
− | To change |
+ | 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> |
<pre> |
||
− | " |
+ | " Replace all Norwegian special characters with entities. |
− | + | nnoremap <silent> ,r :call ReplaceNorChar()<CR> |
|
function! ReplaceNorChar() |
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 |
endfunction |
||
</pre> |
</pre> |
||
Line 107: | Line 118: | ||
Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim. |
Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim. |
||
+ | ==See also== |
||
+ | *[http://www.html-entities.org/ Quick reference and decode/encode tool] |
||
+ | *[http://puzzlersworld.com/misc/html_escape_tool.html Html escape tool] |
||
==Comments== |
==Comments== |
||
Line 182: | Line 196: | ||
endfunction |
endfunction |
||
</pre> |
</pre> |
||
− | <small>--Preceding [[Vim Tips Wiki:Quick reference|unsigned]] comment added by [[User:212.145.191.182|212.145.191.182]] 11:11, July 23, 2010</small> |
||
− | :I formatted the above. What was the dot doing in <tt>s./old/new/eg</tt>? Is the dot supposed to be before the <tt>s</tt> (the current line)? If so, it is redundant because the default is the current line. I removed the dot from each command. [[User:JohnBeckett|JohnBeckett]] 12:07, July 23, 2010 (UTC) |
||
− | |||
− | very well done, thank you |
Latest revision as of 09:14, 13 April 2017
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 (&
).
Simple search and replace[]
This code allows you to easily escape or unescape HTML entities: Change (<
, >
, &
) to (<
, >
, &
), 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 & last execute range . 'sno/</</eg' execute range . 'sno/>/>/eg' execute range . 'sno/&/&/eg' else " must convert & first execute range . 'sno/&/&/eg' execute range . 'sno/</</eg' execute range . 'sno/>/>/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 escaping[]
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::Entities[]
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 HTMLEncode[]
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-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 ,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/Æ/\Æ/eg silent %s/Ø/\Ø/eg silent %s/Å/\Å/eg silent %s/æ/\æ/eg silent %s/ø/\ø/eg silent %s/å/\å/eg endfunction
Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim.
See also[]
Comments[]
Can check it with:
.! php -r "echo htmlentities('<cword>');"
command Entities :call Entities() function Entities() silent s/À/\À/eg silent s/Á/\Á/eg silent s/Â/\Â/eg silent s/Ã/\Ã/eg silent s/Ä/\Ä/eg silent s/Å/\Å/eg silent s/Æ/\Æ/eg silent s/Ç/\Ç/eg silent s/È/\È/eg silent s/É/\É/eg silent s/Ê/\Ê/eg silent s/Ë/\Ë/eg silent s/Ì/\Ì/eg silent s/Í/\Í/eg silent s/Î/\Î/eg silent s/Ï/\Ï/eg silent s/Ð/\Ð/eg silent s/Ñ/\Ñ/eg silent s/Ò/\Ò/eg silent s/Ó/\Ó/eg silent s/Ô/\Ô/eg silent s/Õ/\Õ/eg silent s/Ö/\Ö/eg silent s/Ø/\Ø/eg silent s/Ù/\Ù/eg silent s/Ú/\Ú/eg silent s/Û/\Û/eg silent s/Ü/\Ü/eg silent s/Ý/\Ý/eg silent s/Þ/\Þ/eg silent s/ß/\ß/eg silent s/à/\à/eg silent s/á/\á/eg silent s/â/\â/eg silent s/ã/\ã/eg silent s/ä/\ä/eg silent s/å/\å/eg silent s/æ/\æ/eg silent s/ç/\ç/eg silent s/è/\è/eg silent s/é/\é/eg silent s/ê/\ê/eg silent s/ë/\ë/eg silent s/ì/\ì/eg silent s/í/\í/eg silent s/î/\î/eg silent s/ï/\ï/eg silent s/ð/\ð/eg silent s/ñ/\ñ/eg silent s/ò/\ò/eg silent s/ó/\ó/eg silent s/ô/\ô/eg silent s/õ/\õ/eg silent s/ö/\ö/eg silent s/ø/\ø/eg silent s/ù/\ù/eg silent s/ú/\ú/eg silent s/û/\û/eg silent s/ü/\ü/eg silent s/ý/\ý/eg silent s/þ/\þ/eg silent s/ÿ/\ÿ/eg endfunction