how to set encoding for certain file types only?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how to set encoding for certain file types only?

phyrster
Hi vimmers,

I am using vim to write latex documents under utf-8 locales.
Since my latex suite does not support Chinese under utf8 environment,
every time, I have to manually type:

set fileencoding=cp936 to manually covert the .tex file into Chinese
encoding.

I want to know, how to make vim automatically save all .tex files but .tex
files only with cp936 encoding?

Here is my .vimrc setting, how to make changes to it? (sorry for the long
message body)


------
"multi-language CJK display/multi-encoding
if has("multi_byte")
  "set bomb
  set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1
  " CJK environment detection and corresponding setting
  if v:lang =~ "^zh_CN"
    " Use cp936 to support GBK, euc-cn == gb2312
    set encoding=cp936
    set termencoding=cp936
    set fileencoding=cp936
  elseif v:lang =~ "^zh_TW"
    " cp950, big5 or euc-tw
    " Are they equal to each other?
    set encoding=big5
    set termencoding=big5
    set fileencoding=big5
  elseif v:lang =~ "^ko"
    " Copied from someone's dotfile, untested
    set encoding=euc-kr
    set termencoding=euc-kr
    set fileencoding=euc-kr
  elseif v:lang =~ "^ja_JP"
    " Copied from someone's dotfile, untested
    set encoding=euc-jp
    set termencoding=euc-jp
    set fileencoding=euc-jp
  endif
  " Detect UTF-8 locale, and replace CJK setting if needed
  if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
    set encoding=utf-8
    set termencoding=utf-8
    set fileencoding=utf-8
  endif
else
  echoerr "Sorry, this version of (g)vim was not compiled with multi_byte"
endif
-------



regards

bxuef

--
dhammapada says:
 
  He who by here and now abandoning sensuality, has gone forth a homeless
  wanderer, the search for pleasure extinguished - that is what I call a
  brahmin. 415
 
Reply | Threaded
Open this post in threaded view
|

Re: how to set encoding for certain file types only?

adah
phyrster wrote:

> I am using vim to write latex documents under utf-8 locales.
> Since my latex suite does not support Chinese under utf8 environment,
> every time, I have to manually type:
>
> set fileencoding=cp936 to manually covert the .tex file into Chinese
> encoding.
>
> I want to know, how to make vim automatically save all .tex files but
> .tex files only with cp936 encoding?

Is this what you want?

  function! CheckTexEncoding()
    if &ft == 'tex' && expand('%') !~? '\.tex$'
      set fileencoding=chinese
    endif
  endfunction

  au BufWritePre * call CheckTexEncoding()

Best regards,

Yongwei