:set fileencoding
顯示當前檔案編碼。若常遇亂碼,在 ~/.vimrc 加入:
set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
Vim 將依序嘗試列表編碼,自動識別 UTF-8/GBK。
:set fileencoding=utf-8
存檔即轉為 UTF-8。
iconv -f 原編碼 -t 新編碼 輸入檔 -o 輸出檔 # 例:GBK 轉 UTF-8 iconv -f GBK -t UTF-8 file1 -o file2
跨系統複製中文檔名常因編碼不一致(Windows GBK、Linux UTF-8)產生亂碼,需轉換檔名編碼。
yum -y install convmv
| 參數 | 說明 |
|---|---|
| -f | 來源編碼 |
| -t | 目標編碼 |
| -r | 遞迴處理子目錄 |
| --notest | 真正執行(預設僅模擬) |
| --list | 顯示支援編碼列表 |
| --unescap | 處理逸出字元(如 %20 → 空格) |
convmv -f UTF-8 -t GBK --notest "utf8編碼的檔名"
| 選項 | 功能 |
|---|---|
| encoding | Vim 內部編碼(緩衝區、選單、訊息),建議設 utf-8,只在 .vimrc 修改。 |
| fileencoding | 當前檔案編碼,存檔時依此編碼寫入。 |
| fileencodings | 自動偵測順序列表,優先放 Unicode,最後放 latin1。 |
| termencoding | 終端機編碼,若與 Vim 內部編碼不同需設定,GUI 模式無效。 |
encoding 設定內部編碼。fileencodings 列表偵測,設定 fileencoding。fileencoding != encoding,呼叫 iconv 轉換後載入緩衝區。iconv 轉回 fileencoding 寫入。建議:encoding=utf-8、fileencoding=chinese(Unix 表 GB2312,Windows 表 CP936/GBK),兼顧相容與偵測準確度。