歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> Linux系統攻略 Vim的編碼及字體問題詳解

Linux系統攻略 Vim的編碼及字體問題詳解

日期:2017/2/27 9:50:53   编辑:更多Linux

和所有的流行文本編輯器一樣,Vim 可以很好的編輯各種字符編碼的文件,這當然包括UCS-2、UTF-8 等流行的 Unicode 編碼方式。然而不幸的是,和很多來自 Linux 世界的軟件一樣,這需要你自己動手設置。

Vim 有四個跟字符編碼方式有關的選項,encoding、fileencoding、fileencodings、termencoding (這些選項可能的取值請參考 Vim 在線幫助 :help encoding-names),它們的意義如下:

* encoding: Vim 內部使用的字符編碼方式,包括 Vim 的 buffer (緩沖區)、菜單文本、消息文本等。默認是根據你的locale選擇.用戶手冊上建議只在 .vimrc 中改變它的值,事實上似乎也只有在.vimrc 中改變它的值才有意義。你可以用另外一種編碼來編輯和保存文件,如你的vim的encoding為utf-8,所編輯的文件采用cp936編碼,vim會自動將讀入的文件轉成utf-8(vim的能讀懂的方式),而當你寫入文件時,又會自動轉回成cp936(文件的保存編碼).

* fileencoding: Vim 中當前編輯的文件的字符編碼方式,Vim 保存文件時也會將文件保存為這種字符編碼方式 (不管是否新文件都如此)。

* fileencodings: Vim自動探測fileencoding的順序列表, 啟動時會按照它所列出的字符編碼方式逐一探測即將打開的文件的字符編碼方式,並且將 fileencoding 設置為最終探測到的字符編碼方式。因此最好將Unicode 編碼方式放到這個列表的最前面,將拉丁語系編碼方式 latin1 放到最後面。

* termencoding: Vim 所工作的終端 (或者 Windows 的 Console 窗口) 的字符編碼方式。如果vim所在的term與vim編碼相同,則無需設置。如其不然,你可以用vim的termencoding選項將自動轉換成term的編碼.這個選項在 Windows 下對我們常用的 GUI 模式的 gVim 無效,而對 Console 模式的Vim 而言就是 Windows 控制台的代碼頁,並且通常我們不需要改變它。

好了,解釋完了這一堆容易讓新手犯糊塗的參數,我們來看看 Vim 的多字符編碼方式支持是如何工作的。

1. Vim 啟動,根據 .vimrc 中設置的 encoding 的值來設置 buffer、菜單文本、消息文的字符編碼方式。

2. 讀取需要編輯的文件,根據 fileencodings 中列出的字符編碼方式逐一探測該文件編碼方式。並設置 fileencoding 為探測到的,看起來是正確的 (注1) 字符編碼方式。

3. 對比 fileencoding 和 encoding 的值,若不同則調用 iconv 將文件內容轉換為encoding 所描述的字符編碼方式,並且把轉換後的內容放到為此文件開辟的 buffer 裡,此時我們就可以開始編輯這個文件了。注意,完成這一步動作需要調用外部的 iconv.dll(注2),你需要保證這個文件存在於 $VIMRUNTIME 或者其他列在 PATH 環境變量中的目錄裡。

4. 編輯完成後保存文件時,再次對比 fileencoding 和 encoding 的值。若不同,再次調用 iconv 將即將保存的 buffer 中的文本轉換為 fileencoding 所描述的字符編碼方式,並保存到指定的文件中。同樣,這需要調用 iconv.dll由於 Unicode 能夠包含幾乎所有的語言的字符,而且 Unicode 的 UTF-8 編碼方式又是非常具有性價比的編碼方式 (空間消耗比 UCS-2 小),因此建議 encoding 的值設置為utf-8。這麼做的另一個理由是 encoding 設置為 utf-8 時,Vim 自動探測文件的編碼方式會更准確 (或許這個理由才是主要的 ;)。我們在中文 Windows 裡編輯的文件,為了兼顧與其他軟件的兼容性,文件編碼還是設置為 GB2312/GBK 比較合適,因此 fileencoding 建議設置為 chinese (chinese 是個別名,在 Unix 裡表示 gb2312,在 Windows 裡表示cp936,也就是 GBK 的代碼頁)。

以下是我的 .vimrc(見附件) 中關於字符編碼方式設置的內容,這個設置比較有彈性,可以根據系統中的環境變量 $LANG (當然,Windows 中的寫法是 %LANG%) 的值來自動設置合適的字符編碼方式。此時,推薦設置 %LANG% = zh_CN.UTF-8,可以通過後面的 Windows 注冊表腳本文件來方便的做到。

注1: 事實上,Vim 的探測准確度並不高,尤其是在 encoding 沒有設置為 utf-8 時。因此強烈建議將 encoding 設置為 utf-8,雖然如果你想 Vim 顯示中文菜單和提示消息的話這樣會帶來另一個小問題。

注2: 在 GNU 的 FTP 上可以下載到 iconv 的 Win32 版(http://mirrors.kernel.org/gnu/libiconv/libiconv-1.9.1.bin.woe32.zip),不推薦去GnuWin32(http://gnuwin32.sourceforge.net/) 下載 libiconv,因為那個版本舊一些,並且需要自己改名 dll 文件。

注3: 查看幫助 :h iconv-dynamic

On MS-Windows Vim can be compiled with the +iconv/dyn feature. This means Vim will search for the "iconv.dll" and "libiconv.dll" libraries. When neither of them can be found Vim will still work but some conversions won't be possible.

附1:vimrc文件

" Multi-encoding setting, MUST BE IN THE BEGINNING OF .vimrc!



"
if has("multi_byte")
" When 'fileencodings' starts with 'ucs-bom', don't do this manually
"set bomb
set fileencodings=ucs-bom,chinese,taiwan,japan,korea,utf-8,latin1
" CJK environment detection and corresponding setting
if v:lang =~ "^zh_CN"
" Simplified Chinese, on Unix euc-cn, on MS-Windows cp936
set encoding=chinese
set termencoding=chinese
if &fileencoding == ''
set fileencoding=chinese
endif
elseif v:lang =~ "^zh_TW"
" Traditional Chinese, on Unix euc-tw, on MS-Windows cp950
set encoding=taiwan
set termencoding=taiwan
if &fileencoding == ''
set fileencoding=taiwan
endif
elseif v:lang =~ "^ja_JP"
" Japanese, on Unix euc-jp, on MS-Windows cp932
set encoding=japan
set termencoding=japan
if &fileencoding == ''
set fileencoding=japan
endif
elseif v:lang =~ "^ko"
" Korean on Unix euc-kr, on MS-Windows cp949
set encoding=korea
set termencoding=korea
if &fileencoding == ''
set fileencoding=korea
endif
endif
" Detect UTF-8 locale, and override CJK setting if needed
if v:lang =~ "utf8$" v:lang =~ "UTF-8$"
set encoding=utf-8
endif
else
echoerr 'Sorry, this version of (g)Vim was not compiled with "multi_byte"'
endif

附2:

Supported 'encoding' values are: *encoding-values*


1 latin1 8-bit characters (ISO 8859-1)
1 iso-8859-n ISO_8859 variant (n = 2 to 15)
1 koi8-r Russian
1 koi8-u Ukrainian
1 macroman MacRoman (Macintosh encoding)
1 8bit-{name} any 8-bit encoding (Vim specific name)
1 cp437 similar to iso-8859-1
1 cp737 similar to iso-8859-7
1 cp775 Baltic
1 cp850 similar to iso-8859-4
1 cp852 similar to iso-8859-1
1 cp855 similar to iso-8859-2
1 cp857 similar to iso-8859-5
1 cp860 similar to iso-8859-9
1 cp861 similar to iso-8859-1
1 cp862 similar to iso-8859-1
1 cp863 similar to iso-8859-8
1 cp865 similar to iso-8859-1
1 cp866 similar to iso-8859-5
1 cp869 similar to iso-8859-7
1 cp874 Thai
1 cp1250 Czech, Polish, etc.
1 cp1251 Cyrillic
1 cp1253 Greek
1 cp1254 Turkish
1 cp1255 Hebrew
1 cp1256 Arabic
1 cp1257 Baltic
1 cp1258 Vietnamese
1 cp{number} MS-Windows: any installed single-byte codepage
2 cp932 Japanese (Windows only)
2 euc-jp Japanese (Unix only)
2 sjis Japanese (Unix only)
2 cp949 Korean (Unix and Windows)
2 euc-kr Korean (Unix only)
2 cp936 simplified Chinese (Windows only)
2 euc-cn simplified Chinese (Unix only)
2 cp950 traditional Chinese (on Unix alias for big5)
2 big5 traditional Chinese (on Windows alias for cp950)
2 euc-tw traditional Chinese (Unix only)


2 2byte-{name} Unix: any double-byte encoding (Vim specific name)
2 cp{number} MS-Windows: any installed double-byte codepage
u utf-8 32 bit UTF-8 encoded Unicode (ISO/IEC 10646-1)
u ucs-2 16 bit UCS-2 encoded Unicode (ISO/IEC 10646-1)
u ucs-2le like ucs-2, little endian
u utf-16 ucs-2 extended with double-Words for more characters
u utf-16le like utf-16, little endian
u ucs-4 32 bit UCS-4 encoded Unicode (ISO/IEC 10646-1)
u ucs-4le like ucs-4, little endian
The {name} can be any encoding name that your system supports. It is passed
to iconv() to convert between the encoding of the file and the current locale.
For MS-Windows "cp{number}" means using codepage {number}.
Several aliases can be used, they are translated to one of the names above.
An incomplete list:
1 ansi same as latin1 (obsolete, for backward compatibility)
2 japan Japanese: on Unix "euc-jp", on MS-Windows cp932
2 korea Korean: on Unix "euc-kr", on MS-Windows cp949
2 prc simplified Chinese: on Unix "euc-cn", on MS-Windows cp936
2 chinese same as "prc"
2 taiwan traditional Chinese: on Unix "euc-tw", on MS-Windows cp950
u utf8 same as utf-8
u unicode same as ucs-2
u ucs2be same as ucs-2 (big endian)
u ucs-2be same as ucs-2 (big endian)
u ucs-4be same as ucs-4 (big endian)
default stands for the default value of 'encoding', depends on the
environment



Copyright © Linux教程網 All Rights Reserved