vim encoding and font

來源:互聯網
上載者:User

一般的,vim開啟中文檔案時會出現亂碼,原因比較複雜,不羅嗦了。直接講解決辦法
    set fileencoding=gb18030
    set fileencodings=utf-8,gb18030,utf-16,big5
想看這樣設定的原因嗎?請繼續。下文在網路中廣泛流傳

vim裡面的編碼主要跟三個參數有關:enc(encoding), fenc(fileencoding)和fencs(fileencodings)

fenc是當前檔案的編碼,也就是說,一個在vim裡面已經正確顯示了的檔案(前提是你的系統內容跟你的enc設定匹配),你可以通過改變 fenc後再w來將此檔案存成不同的編碼。比如說,我:set fenc=utf-8然後:w就把檔案存成utf-8的了,:set fenc=gb18030再:w就把檔案存成gb18030的了。這個值對於開啟檔案的時候是否能夠正確地解碼沒有任何關係。

fencs就是用來在開啟檔案的時候進行解碼的猜測列表。檔案編碼沒有百分百正確的判斷方法,所以vim只能猜測檔案編碼。比如我的vimrc裡面這個的設定是
set fileencodings=utf-8,gb18030,utf-16,big5

所以我的vim每開啟一個檔案,先嘗試用utf-8進行解碼,如果用utf-8解碼到了一半出錯(所謂出錯的意思是某個地方無法用utf-8正確地解碼),那麼就從頭來用gb18030重新嘗試解碼,如果gb18030又出錯(注意gb18030並不是像utf-8似的規則編碼,所以所謂的出錯只是說某個編碼沒有對應的有意義的字,比如0),就嘗試用utf-16,仍然出錯就嘗試用big5。這一趟下來,如果中間的某次解碼從頭到尾都沒有出錯,那麼 vim就認為這個檔案是這個編碼的,不會再進行後面的嘗試了。這個時候,fenc的值就會被設為vim最後採用的編碼值,可以用:set fenc?來查看具體是什麼。

當然這個也是有可能出錯的,比如你的檔案是gb18030編碼的,但是實際上只有一兩個字元是中文,那麼有可能他們正好也能被utf-8解碼,那麼這個檔案就會被誤認為是utf-8的導致錯誤解碼。

enc其作用基本只是顯示。不管最後的檔案是什麼編碼的,vim都會將其轉換為當前系統編碼來進行處理,這樣才能在當前系統裡面正確地顯示出來,因此enc就是幹這個的。在windows下面,enc預設是cp936,這也就是中文windows的預設編碼,所以enc是不需要改的。在 linux下,隨著你的系統locale可能設為zh_CN.gb18030或者zh_CN.utf-8,你的enc要對應的設為gb18030或者 utf-8(或者gbk之類的)。

最後再來說一下建立空檔案的預設編碼。看文檔好像說會採用fencs裡面的第一個編碼作為建立檔案的預設編碼。但是這裡有一個問題,就是fencs 的順序跟解碼成功率有很大關係,根據我的經驗utf-8在前比gb18030在前成功率要高一些,那麼如果我建立檔案預設想讓它是gb18030編碼怎麼辦?一個方法是每次建立檔案後都:set fenc=gb18030一下,不過我發現在vimrc裡面設定fenc=gb18030也能達到這個效果。

另外,在ubuntu中文論壇還有人提出了這樣的辦法,直接就配置了

    所有代碼直接粘貼到終端運行即可!
    安裝程式
    代碼:
    sudo apt-get install vim-gtk vim-doc cscope

    建立啟動項
    代碼:

    cat > /usr/share/applications/gvim.desktop < $HOME/.vimrc << “EOF”
    “===========================================================================
    ” 項目: gvim 設定檔
    ” 作者: yonsan [QQ:82555472]
    ” 安裝: sudo apt-get install vim-gtk
    ” 用法: 將本檔案(.vimrc)拷貝到$HOME/
    “===========================================================================

    ” 使用 murphy 調色盤
    colo murphy
    ” 設定用於GUI圖形化使用者介面的字型列表。
    set guifont=SimSun 10
    ”
    set nocompatible
    ” 設定檔案瀏覽器目錄為目前的目錄
    set bsdir=buffer
    set autochdir
    ” 設定編碼
    set enc=utf-8
    ” 設定檔案編碼
    set fenc=utf-8
    ” 設定檔案編碼檢測類型及支援格式
    set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
    ” 指定菜單語言
    set langmenu=zh_CN.UTF-8
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim
    ” 設定文法高亮度
    set syn=cpp
    “顯示行號
    set nu!
    ” 尋找結果高亮度顯示
    set hlsearch
    ” tab寬度
    set tabstop=4
    set cindent shiftwidth=4
    set autoindent shiftwidth=4
    ” C/C++注釋
    set comments=://
    ” 修正自動C式樣注釋功能
    set comments=s1:/*,mb:*,ex0:/
    ” 增強檢索功能
    set tags=./tags,./../tags,./**/tags
    ” 儲存檔案格式
    set fileformats=unix,dos
    ” 鍵盤操作
    map gk
    map gj
    ” 命令列高度
    set cmdheight=1
    ” 使用cscope
    if has(”cscope”)
    set csprg=/usr/bin/cscope
    set csto=0
    set cst
    set nocsverb
    ” add any database in current directory
    if filereadable(”cscope.out”)
    cs add cscope.out
    ” else add database pointed to by environment
    elseif $CSCOPE_DB != “”
    cs add $CSCOPE_DB
    endif
    set csverb
    endi
    ” 中文協助
    if version > 603
    set helplang=cn
    endi
    EOF

    locale為zh_CN.gbk的設定檔
    代碼:

    cat > $HOME/.vimrc << “EOF”
    “===========================================================================
    ” 項目: gvim 設定檔
    ” 作者: yonsan [QQ:82555472]
    ” 安裝: sudo apt-get install vim-gtk
    ” 用法: 將本檔案(.vimrc)拷貝到$HOME/
    “===========================================================================

    ” 使用 murphy 調色盤
    colo murphy
    ” 設定用於GUI圖形化使用者介面的字型列表。
    set guifont=SimSun 10
    ”
    set nocompatible
    ” 設定檔案瀏覽器目錄為目前的目錄
    set bsdir=buffer
    set autochdir
    ” 設定編碼
    set enc=chinese
    ” 設定檔案編碼
    set fenc=chinese
    ” 設定檔案編碼檢測類型及支援格式
    set fencs=gbk,utf-8,ucs-bom,gb18030,gb2312,cp936
    ” 指定菜單語言
    set langmenu=zh_CN.GBK
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim
    ” 設定文法高亮度
    set syn=cpp
    “顯示行號
    set nu!
    ” 尋找結果高亮度顯示
    set hlsearch
    ” tab寬度
    set tabstop=4
    set cindent shiftwidth=4
    set autoindent shiftwidth=4
    ” C/C++注釋
    set comments=://
    ” 修正自動C式樣注釋功能
    set comments=s1:/*,mb:*,ex0:/
    ” 增強檢索功能
    set tags=./tags,./../tags,./**/tags
    ” 儲存檔案格式
    set fileformats=unix,dos
    ” 鍵盤操作
    map gk
    map gj
    ” 命令列高度
    set cmdheight=1
    ” 使用cscope
    if has(”cscope”)
    set csprg=/usr/bin/cscope
    set csto=0
    set cst
    set nocsverb
    ” add any database in current directory
    if filereadable(”cscope.out”)
    cs add cscope.out
    ” else add database pointed to by environment
    elseif $CSCOPE_DB != “”
    cs add $CSCOPE_DB
    endif
    set csverb
    endi
    ” 中文協助
    if version > 603
    set helplang=cn
    endi
    EOF


和所有的流行文字編輯器一樣,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

 

轉載聲明: 本文轉自 http://www.cnblogs.com/h2appy/archive/2008/08/14/1267593.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.