Vim Tips Wiki
Register
(Move categories to tip template)
(Remove html character entities)
Line 15: Line 15:
   
 
<pre>
 
<pre>
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;
+
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 
</pre>
 
</pre>
   
Line 35: Line 35:
   
 
function! DetectHtmlEncoding()
 
function! DetectHtmlEncoding()
if &amp;filetype != 'html'
+
if &filetype != 'html'
 
return
 
return
 
endif
 
endif
 
normal m`
 
normal m`
 
normal gg
 
normal gg
if search('\c&lt;meta http-equiv=\("\?\)Content-Type\1 content="text/html; charset=[-A-Za-z0-9_]\+"&gt;') != 0
+
if search('\c<meta http-equiv=\("\?\)Content-Type\1 content="text/html; charset=[-A-Za-z0-9_]\+">') != 0
 
let reg_bak=@"
 
let reg_bak=@"
 
normal y$
 
normal y$
Line 47: Line 47:
 
normal ``
 
normal ``
 
let @"=reg_bak
 
let @"=reg_bak
if &amp;fileencodings == ''
+
if &fileencodings == ''
let auto_encodings=',' . &amp;encoding . ','
+
let auto_encodings=',' . &encoding . ','
 
else
 
else
let auto_encodings=',' . &amp;fileencodings . ','
+
let auto_encodings=',' . &fileencodings . ','
 
endif
 
endif
if charset !=? &amp;fileencoding &amp;&amp;
+
if charset !=? &fileencoding &&
\auto_encodings =~ ',' . &amp;fileencoding . ','
+
\auto_encodings =~ ',' . &fileencoding . ','
 
silent! exec 'e ++enc=' . charset
 
silent! exec 'e ++enc=' . charset
 
endif
 
endif
Line 71: Line 71:
   
 
==Comments==
 
==Comments==
 
----
 

Revision as of 23:51, 29 September 2008

Tip 1074 Printable Monobook Previous Next

created December 9, 2005 · complexity advanced · author Wu Yongwei · version 6.0


If one needs to edit files encoded in multiple legacy encodings, then the Vim fileencodings option cannot help much. Some hacks can be used to put the file encoding in the file (see VimTip911). However, in the case of HTML files, the encoding information is often in the HTML file already, especially for non-Latin1 Web pages, for example:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

The following code can be put in vimrc to detect and use such an encoding specification:

if has('autocmd')
  function! ConvertHtmlEncoding(encoding)
    if a:encoding ==? 'gb2312'
      return 'cp936' " GB2312 imprecisely means CP936 in HTML
    elseif a:encoding ==? 'iso-8859-1'
      return 'latin1' " The canonical encoding name in Vim
    elseif a:encoding ==? 'utf8'
      return 'utf-8' " Other encoding aliases should follow here
    else
      return a:encoding
    endif
  endfunction

  function! DetectHtmlEncoding()
    if &filetype != 'html'
      return
    endif
    normal m`
    normal gg
    if search('\c<meta http-equiv=\("\?\)Content-Type\1 content="text/html; charset=[-A-Za-z0-9_]\+">') != 0
      let reg_bak=@"
      normal y$
      let charset=matchstr(@", 'text/html; charset=\zs[-A-Za-z0-9_]\+')
      let charset=ConvertHtmlEncoding(charset)
      normal ``
      let @"=reg_bak
      if &fileencodings == ''
        let auto_encodings=',' . &encoding . ','
      else
        let auto_encodings=',' . &fileencodings . ','
      endif
      if charset !=? &fileencoding &&
            \auto_encodings =~ ',' . &fileencoding . ','
        silent! exec 'e ++enc=' . charset
      endif
    else
      normal ``
    endif
  endfunction

  " Detect charset encoding in an HTML file
  au BufReadPost *.htm* nested call DetectHtmlEncoding()
endif

Please notice that the nested autocommand is used to ensure the syntax highlighting is OK and the remembered cursor position is still kept.

It is recommended to use set encoding=utf-8 in order to ensure successful encoding conversion.

Comments