Wikia

Vim Tips Wiki

Changes: HTML entities

Edit

Back to page

(Clarify that the simple maps only work in normal mode.)
(The current line is affected, but replace with better code.)
 
Line 14: Line 14:
   
 
==Simple search and replace==
 
==Simple search and replace==
This code allows you to escape your HTML entities with one shortcut key: Change (<code><</code>, <code>></code>, <code>&</code>) to (<code>&amp;lt;</code>, <code>&amp;gt;</code>, <code>&amp;amp;</code>), or the reverse.
+
This code allows you to easily escape or unescape HTML entities: Change (<code><</code>, <code>></code>, <code>&</code>) to (<code>&amp;lt;</code>, <code>&amp;gt;</code>, <code>&amp;amp;</code>), or the reverse.
   
 
This does not escape all characters that should be escaped—just the most common.
 
This does not escape all characters that should be escaped—just the most common.
 
<pre>
 
<pre>
function HtmlEscape()
+
" Escape/unescape & < > HTML entities in range (default current line).
silent s/&/\&amp;amp;/eg
+
function! HtmlEntities(line1, line2, action)
silent s/</\&amp;lt;/eg
+
let search = @/
silent s/>/\&amp;gt;/eg
+
let range = 'silent ' . a:line1 . ',' . a:line2
  +
if a:action == 0 " must convert &amp;amp; last
  +
execute range . 'sno/&amp;lt;/</eg'
  +
execute range . 'sno/&amp;gt;/>/eg'
  +
execute range . 'sno/&amp;amp;/&/eg'
  +
else " must convert & first
  +
execute range . 'sno/&/&amp;amp;/eg'
  +
execute range . 'sno/</&amp;lt;/eg'
  +
execute range . 'sno/>/&amp;gt;/eg'
  +
endif
  +
nohl
  +
let @/ = search
 
endfunction
 
endfunction
  +
command! -range -nargs=1 Entities call HtmlEntities(<line1>, <line2>, <args>)
  +
noremap <silent> \h :Entities 0<CR>
  +
noremap <silent> \H :Entities 1<CR>
  +
</pre>
   
function HtmlUnEscape()
+
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.
silent s/&amp;lt;/</eg
 
silent s/&amp;gt;/>/eg
 
silent s/&amp;amp;/\&/eg
 
endfunction
 
   
nnoremap <Leader>h :call HtmlEscape()<CR>
+
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:
nnoremap <Leader>H :call HtmlUnEscape()<CR>
+
<pre>
</pre>
+
" Unescape lines 10 to 20 inclusive.
  +
:10,20Entities 0
   
If you add this code to your [[vimrc]], you can HTML escape the character under the cursor by typing <code>\h</code>, and unescape by typing <code>\H</code> (assuming the default backslash leader key).
+
" Escape all lines.
  +
:%Entities 1
  +
</pre>
   
 
==Automagic escaping==
 
==Automagic escaping==
Line 99: Line 103:
   
 
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 174: Line 182:
 
endfunction
 
endfunction
 
</pre>
 
</pre>
 
==Online HTML Escape Tool==
 
There are various online HTML Escape Tools available, they can convert any html text to escaped text. You can check one of such tool [http://puzzlersworld.com/misc/html_escape_tool.html Html Escape Tool]
 
 
==HTML entities quick reference==
 
Quick reference (http://www.html-entities.org/)
 

Latest revision as of 07:33, November 1, 2012

Tip 1005 Printable Monobook Previous Next

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 (&amp;).

Simple search and replaceEdit

This code allows you to easily escape or unescape HTML entities: Change (<, >, &) to (&lt;, &gt;, &amp;), 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 &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
command! -range -nargs=1 Entities call HtmlEntities(<line1>, <line2>, <args>)
noremap <silent> \h :Entities 0<CR>
noremap <silent> \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 escapingEdit

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::EntitiesEdit

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 HTMLEncodeEdit

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-entitiesEdit

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/Æ/\&AElig;/eg
  silent %s/Ø/\&Oslash;/eg
  silent %s/Å/\&Aring;/eg
  silent %s/æ/\&aelig;/eg
  silent %s/ø/\&oslash;/eg
  silent %s/å/\&aring;/eg
endfunction

Add it to your ~/.vimrc or ~/.vim/ftplugin/html.vim.

See alsoEdit

CommentsEdit

Can check it with:

.! php -r "echo htmlentities('<cword>');"

command Entities :call Entities()
function Entities()
  silent s/À/\&Agrave;/eg
  silent s/Á/\&Aacute;/eg
  silent s/Â/\&Acirc;/eg
  silent s/Ã/\&Atilde;/eg
  silent s/Ä/\&Auml;/eg
  silent s/Å/\&Aring;/eg
  silent s/Æ/\&AElig;/eg
  silent s/Ç/\&Ccedil;/eg
  silent s/È/\&Egrave;/eg
  silent s/É/\&Eacute;/eg
  silent s/Ê/\&Ecirc;/eg
  silent s/Ë/\&Euml;/eg
  silent s/Ì/\&Igrave;/eg
  silent s/Í/\&Iacute;/eg
  silent s/Î/\&Icirc;/eg
  silent s/Ï/\&Iuml;/eg
  silent s/Ð/\&ETH;/eg
  silent s/Ñ/\&Ntilde;/eg
  silent s/Ò/\&Ograve;/eg
  silent s/Ó/\&Oacute;/eg
  silent s/Ô/\&Ocirc;/eg
  silent s/Õ/\&Otilde;/eg
  silent s/Ö/\&Ouml;/eg
  silent s/Ø/\&Oslash;/eg
  silent s/Ù/\&Ugrave;/eg
  silent s/Ú/\&Uacute;/eg
  silent s/Û/\&Ucirc;/eg
  silent s/Ü/\&Uuml;/eg
  silent s/Ý/\&Yacute;/eg
  silent s/Þ/\&THORN;/eg
  silent s/ß/\&szlig;/eg
  silent s/à/\&agrave;/eg
  silent s/á/\&aacute;/eg
  silent s/â/\&acirc;/eg
  silent s/ã/\&atilde;/eg
  silent s/ä/\&auml;/eg
  silent s/å/\&aring;/eg
  silent s/æ/\&aelig;/eg
  silent s/ç/\&ccedil;/eg
  silent s/è/\&egrave;/eg
  silent s/é/\&eacute;/eg
  silent s/ê/\&ecirc;/eg
  silent s/ë/\&euml;/eg
  silent s/ì/\&igrave;/eg
  silent s/í/\&iacute;/eg
  silent s/î/\&icirc;/eg
  silent s/ï/\&iuml;/eg
  silent s/ð/\&eth;/eg
  silent s/ñ/\&ntilde;/eg
  silent s/ò/\&ograve;/eg
  silent s/ó/\&oacute;/eg
  silent s/ô/\&ocirc;/eg
  silent s/õ/\&otilde;/eg
  silent s/ö/\&ouml;/eg
  silent s/ø/\&oslash;/eg
  silent s/ù/\&ugrave;/eg
  silent s/ú/\&uacute;/eg
  silent s/û/\&ucirc;/eg
  silent s/ü/\&uuml;/eg
  silent s/ý/\&yacute;/eg
  silent s/þ/\&thorn;/eg
  silent s/ÿ/\&yuml;/eg
endfunction

Around Wikia's network

Random Wiki