简体   繁体   English

如何在vim中“自动”将euc-kr转换为utf-8?

[英]how to convert euc-kr to utf-8 'automatically' in vim?

I know how to convert it manually like this: map <F4> :e ++enc=euc-kr <CR> :set fenc=utf-8 <CR> :w ++enc=utf-8 <CR> . 我知道如何手动将其转换为: map <F4> :e ++enc=euc-kr <CR> :set fenc=utf-8 <CR> :w ++enc=utf-8 <CR>

But, I want to convert automatically by writing conditional-statement inside .vimrc file. 但是,我想通过在.vimrc文件中写入条件语句来自动进行转换。 So, I wrote the code as below and it didn't work. 所以,我写了下面的代码,但是没有用。

    if ($fileencoding == "euc-kr")
        ++enc=euc-kr %
        set fenc=utf-8
    endif

What was wrong and how to fix it? 出了什么问题以及如何解决?

There are two parts to it: 它包括两个部分:

To allow Vim to detect the encoding automatically on :edit (so that you don't need to specify ++enc=euc-kr ), you have to adapt your 'filencodings' (plural!) option, eg: 为了允许Vim在:edit上自动检测编码(这样就无需指定++enc=euc-kr ),您必须调整'filencodings' (复数!)选项,例如:

:set fileencodings=ucs-bom,utf-8,euc-kr

In order to persist the encoding as UTF-8, the following autocmd will adapt the 'fileencoding' (singluar) for the current buffer: 为了将编码保持为UTF-8,以下autocmd将为当前缓冲区改编'fileencoding' (singluar):

:autocmd BufWritePre * if &l:fileencoding ==# 'euc-kr' | setlocal fileencoding=utf-8 | endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM