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 Git Bash 中文亂碼徹底解決方案:編碼、顯示與 Vim 一站式配置

編輯:關於 Linux

現象

  • git log、git status 顯示中文檔名亂碼(如 \346\227\245)。
  • ls 列出中文目錄顯示問號或亂碼。
  • Vim 編輯中文檔案出現亂碼或無法輸入。

核心配置檔 /etc/gitconfig(系統級)或 ~/.gitconfig(用戶級)

[core]
    quotepath = false          # 不對中文檔名進行八進位轉義
    editor = vim               # 預設編輯器

[gui]
    encoding = utf-8           # git gui 介面編碼

[i18n]
    commitencoding = utf-8     # 提交訊息編碼(建議統一 UTF-8)
    logoutputencoding = utf-8  # log 輸出編碼

[svn]
    pathnameencoding = utf-8   # git-svn 路徑編碼

Bash 顯示修正 /etc/git-completion.bash 或 ~/.bashrc

# 讓 ls 正常顯示中文
alias ls='ls --show-control-chars --color=auto'

# 環境變數強制 UTF-8(視系統 locale 而定)
export LANG=zh_TW.UTF-8
export LC_ALL=zh_TW.UTF-8

Vim 專用配置 ~/.vimrc

set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8,cp950,big5,gbk,gb2312,gb18030
set termencoding=utf-8
set ambiwidth=double           # 雙寬字元對齊
set number
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
syntax on

Windows 客戶端額外設定

  • Git for Windows 安裝時選擇 Checkout as-is, commit as-is(不轉換換行)。
  • 註冊表 HKEY_CURRENT_USER\Console\%SystemRoot%_System32_cmd.exe 新增 CodePage = 65001(需重開終端機)。

驗證清單

# 1. 檢查 git 設定生效
git config --list | grep -E 'encoding|quotepath'

# 2. 建立中文檔名測試
touch "測試檔案.txt"
git add .
git commit -m "新增中文檔名"
git log --oneline -1
ls -l

套用上述配置後,Git Bash、Vim、圖形介面皆能完美支援繁體/簡體中文,徹底告別亂碼困擾。

Copyright © Windows教程網 All Rights Reserved