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

Cygwin 中文支援完整配置指南 (顯示與輸入)

編輯:Linux 教學

Cygwin 提供 Windows 下的 Linux 模擬環境,預設不支援中文顯示與輸入。本文詳細說明透過修改環境變數、配置檔實現完整中文支援的步驟。

一、核心配置檔案位置

用戶主目錄 ~ (通常為 C:\cygwin\home\用戶名\) 下的三個關鍵檔案:

  • .bash_profile - 登入 Shell 載入
  • .bashrc - 互動式 Shell 載入
  • .inputrc - Readline 庫行編輯行為控制

二、配置 .bash_profile (環境變數與別名)

# ~/.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 (Readline 中文編輯支援)

# ~/.inputrc
set meta-flag on
set convert-meta off
set output-meta on
set input-meta on

這四項設定確保命令列編輯、歷史指令、Tab 補全正常處理中文。

四、配置 .bashrc (互動式 Shell 共用設定)

# ~/.bashrc 末尾加入
source ~/.bash_profile

或直接將上述環境變數複製到 .bashrc。

五、變更 Home 目錄時的額外設定

若自訂 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 終端機優化 (推薦替代預設 cmd 視窗)

  • 安裝 mintty 套件 (Cygwin 安裝器可選)
  • 右鍵選單 → Options → Text → Locale: zh_TW, Character set: UTF-8
  • 支援真彩色、更好的字型渲染、複製貼上體驗優於 cmd
Copyright © Windows教程網 All Rights Reserved