Cygwin 提供 Windows 下的 Linux 模擬環境,預設不支援中文顯示與輸入。本文詳細說明透過修改環境變數、配置檔實現完整中文支援的步驟。
用戶主目錄 ~ (通常為 C:\cygwin\home\用戶名\) 下的三個關鍵檔案:
.bash_profile - 登入 Shell 載入.bashrc - 互動式 Shell 載入.inputrc - Readline 庫行編輯行為控制# ~/.bash_profile 末尾加入
alias ls='ls --color --show-control-chars'
export LC_ALL=zh_TW.UTF-8
export LC_CTYPE=zh_TW.UTF-8
export LANG=zh_TW.UTF-8
export XMODIFIERS="@im=Chinput"
stty cs8 -istrip
stty pass8
export LESSCHARSET=utf-8
說明:
--show-control-chars:讓 ls 正確顯示中文檔名zh_TW.UTF-8 (繁體) 或 zh_CN.UTF-8 (簡體),避免 GB2312/Big5 相容性問題stty cs8 -istrip / stty pass8:設定 8-bit 字元傳遞LESSCHARSET=utf-8:讓 less 指令正確顯示中文# ~/.inputrc
set meta-flag on
set convert-meta off
set output-meta on
set input-meta on
這四項設定確保命令列編輯、歷史指令、Tab 補全正常處理中文。
# ~/.bashrc 末尾加入
source ~/.bash_profile
或直接將上述環境變數複製到 .bashrc。
若自訂 Home 目錄路徑,需修改 cygwin.bat:
@echo off
C:
chdir C:\cygwin\bin
set HOME=C:\cygwin\home
bash --login -i
# 測試中文檔名顯示
touch "測試檔案.txt"
ls -la
# 測試中文輸入
echo "中文測試"
# 測試編輯器
nano 中文檔名.txt
| 問題 | 解決方案 |
|---|---|
| ls 顯示問號 ??? | 確認 --show-control-chars 別名生效,語系設為 UTF-8 |
| 中文輸入亂碼 | 檢查 .inputrc 四項 meta 設定,終端機字編碼設為 UTF-8 |
| nano 儲存中文檔名失敗 | 確認 LANG/LC_ALL 為 UTF-8,非 GB2312/Big5 |
| Python 腳本中文編碼錯誤 | 檔案頭加入 # -*- coding: utf-8 -*- |
mintty 套件 (Cygwin 安裝器可選)