在 Linux 下使用 Git Bash 時,常遭遇中文檔名、log 訊息、vim 編輯顯示亂碼。本文提供完整的編碼設定,涵蓋 Git 設定、Bash alias、vim 設定,讓中文完美顯示。
/etc/gitconfig[gui]
encoding = utf-8
[i18n]
commitencoding = GB2312
[svn]
pathnameencoding = GB2312
gui.encoding:Git GUI 介面顯示編碼。i18n.commitencoding:commit log 編碼,Windows 預設 GB2312,聲明後推送到伺服器不亂碼。svn.pathnameencoding:支援中文路徑。/etc/git-completion.bash檔案末尾加入:
alias ls='ls --show-control-chars --color=auto'
~/.vimrcset nu
set fileencoding=utf-8
set fileencodings=utf-8,gb18030,gb2312,gbk,big5
set ts=4
set expandtab
set autoindent
fileencodings:自動偵測優先順序,最後 fallback 到 latin1。重新啟動 Git Bash,執行 ls、git log、vim 中文檔案,中文應正常顯示。
亂碼本質是編碼不一致:檔案實際編碼、終端編碼、Git 內部編碼三者需一致。上述設定統一以 UTF-8 為主,相容 GB 系編碼。
