Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 檔案編碼檢測、轉換與檔名編碼轉換完整指南

編輯:Linux 教學

檔案編碼檢測

Vim 內建檢測

:set fileencoding

顯示當前檔案編碼。若常遇亂碼,在 ~/.vimrc 加入:

set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

Vim 將依序嘗試列表編碼,自動識別 UTF-8/GBK。

檔案內容編碼轉換

Vim 即時轉換

:set fileencoding=utf-8

存檔即轉為 UTF-8。

iconv 批次轉換

iconv -f 原編碼 -t 新編碼 輸入檔 -o 輸出檔
# 例:GBK 轉 UTF-8
iconv -f GBK -t UTF-8 file1 -o file2

檔名編碼轉換:convmv

跨系統複製中文檔名常因編碼不一致(Windows GBK、Linux UTF-8)產生亂碼,需轉換檔名編碼。

安裝

yum -y install convmv

常用參數

參數說明
-f來源編碼
-t目標編碼
-r遞迴處理子目錄
--notest真正執行(預設僅模擬)
--list顯示支援編碼列表
--unescap處理逸出字元(如 %20 → 空格)

範例:UTF-8 檔名轉 GBK

convmv -f UTF-8 -t GBK --notest "utf8編碼的檔名"

Vim 編碼相關選項深度解析

選項功能
encodingVim 內部編碼(緩衝區、選單、訊息),建議設 utf-8,只在 .vimrc 修改。
fileencoding當前檔案編碼,存檔時依此編碼寫入。
fileencodings自動偵測順序列表,優先放 Unicode,最後放 latin1。
termencoding終端機編碼,若與 Vim 內部編碼不同需設定,GUI 模式無效。

Vim 多編碼工作流程

  1. 啟動,依 encoding 設定內部編碼。
  2. 讀檔,依 fileencodings 列表偵測,設定 fileencoding。
  3. 若 fileencoding != encoding,呼叫 iconv 轉換後載入緩衝區。
  4. 編輯完成存檔,若編碼不同,再呼叫 iconv 轉回 fileencoding 寫入。
建議:encoding=utf-8、fileencoding=chinese(Unix 表 GB2312,Windows 表 CP936/GBK),兼顧相容與偵測準確度。
Copyright © Windows教程網 All Rights Reserved