Codex 修改文件后中文乱码问题:根源在终端编码,不在 VS Code

余胜军说java助理
2026-04-16 11:53
阅读 1352

解决 VS Code + PowerShell 中文乱码终极方案(UTF-8 无 BOM)

原文链接:https://www.cnblogs.com/gccbuaa/p/19227315


一、问题根源

VS Code 与 PowerShell 中文乱码的核心原因,是编码不统一

  • VS Code 默认使用 UTF-8(无 BOM) 编码
  • Windows 自带的 PowerShell 5.1 默认使用 GBK(代码页 936) 编码
  • 两者编码不匹配,导致终端输出、文件读写出现乱码、问号等问题

二、完整解决方案

第一步:配置 VS Code 为 UTF-8

打开 VS Code 设置(快捷键 Ctrl + ,),切换到 settings.json,添加以下配置:

{
  "files.encoding": "utf8",
  "files.autoGuessEncoding": true
}
  • "files.encoding": "utf8":默认以 UTF-8 保存文件
  • "files.autoGuessEncoding": true:打开文件时自动检测编码(尤其对带 BOM 的文件友好)

✅ 这一步确保 VS Code 读写一致。


第二步:配置 PowerShell 终端为 UTF-8(重点!)

1. 确认你用的是哪个 PowerShell?

在终端运行:

$PSVersionTable.PSVersion
  • 5.x → Windows 自带的 Windows PowerShell(旧版)
  • 7.xPowerShell 7+(推荐升级),对 UTF-8 支持更好

强烈建议升级到 PowerShell 7,UTF-8 原生支持更完善。

2. 找到你的 Profile 文件路径

在终端运行:

$PROFILE

常见路径:

  • Windows PowerShell 5.1C:\Users\<用户名>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  • PowerShell 7+C:\Users\<用户名>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

3. 使用安全脚本配置 UTF-8(无 BOM)

⚠️ 注意:不要用 Add-Content -Encoding UTF8,它会写入带 BOM 的 UTF-8,可能引发其他工具兼容问题。我们需要的是 UTF-8 无 BOM

运行以下完整脚本(支持幂等、避免重复、兼容 Win7/10/11):

# Setup-UTF8PowerShellProfile.ps1 - 一键配置 PowerShell UTF-8 无 BOM 编码
$utf8Config = @'
# 设置控制台使用 UTF-8 编码(无 BOM)
chcp 65001 | Out-Null
[Console]::InputEncoding  = [System.Text.UTF8Encoding]::new()
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
$OutputEncoding = [System.Text.UTF8Encoding]::new()

# 设置默认文件操作编码为 UTF-8(无 BOM)
$PSDefaultParameterValues['Out-File:Encoding']    = 'utf8'
$PSDefaultParameterValues['Set-Content:Encoding'] = 'utf8'
$PSDefaultParameterValues['Add-Content:Encoding'] = 'utf8'
'@

# 创建目录(如果不存在)
$profileDir = Split-Path -Parent $PROFILE
if (-not (Test-Path $profileDir)) {
    New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
}

# 读取现有内容
$existing = if (Test-Path $PROFILE) { Get-Content $PROFILE -Raw } else { "" }

# 检查是否已配置(避免重复)
$hasConfig = $existing -match 'Console::OutputEncoding' -and
             $existing -match '\$OutputEncoding' -and
             $existing -match "PSDefaultParameterValues.*Out-File:Encoding"

if (-not $hasConfig) {
    $newContent = if ($existing) { "$existing`r`n$utf8Config" } else { $utf8Config }
    $utf8NoBom = New-Object System.Text.UTF8Encoding($false)  # $false = 无 BOM
    [System.IO.File]::WriteAllText($PROFILE, $newContent, $utf8NoBom)
    Write-Host "✅ UTF-8 配置已写入 Profile!路径: $PROFILE" -ForegroundColor Green
} else {
    Write-Host "ℹ️ UTF-8 已配置,跳过。" -ForegroundColor Cyan
}

# 立即生效
. $PROFILE

三、验证是否生效

1. 终端验证

重启 PowerShell 终端,执行:

# 查看当前代码页(应显示 65001,即 UTF-8)
chcp

# 测试中文输出
"中文测试:你好,PowerShell UTF-8 配置成功!"

2. 文件验证

# 写入中文文件
"中文测试文件" | Out-File test_utf8.txt

# 读取文件,中文正常显示即成功
Get-Content test_utf8.txt

四、关键说明

1. 为什么用这个脚本?

  • 无 BOM 写入:使用 UTF8Encoding($false) 生成标准 UTF-8,完美兼容 VS Code、Linux、Git 等所有环境
  • 幂等执行:重复运行不会重复写入配置,避免环境变量冲突
  • 自动适配:自动识别当前 PowerShell 版本(5.1/7.x)的 Profile 路径,无需手动修改
  • 立即生效:脚本最后执行 . $PROFILE,无需重启终端即可生效

2. 常见问题

  • 脚本执行报错? 首次运行可能遇到执行策略限制,执行以下命令解锁:
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    
  • PowerShell 7 不生效? 打开 pwsh(PowerShell 7),再运行一次脚本,会自动配置 7 的 Profile。
  • CMD 会受影响吗? 完全不会!脚本仅修改 PowerShell 专属配置,CMD 保持原有编码,互不干扰。

五、总结

  1. VS Code 配置 UTF-8,统一文件编码
  2. PowerShell 脚本配置 UTF-8 无 BOM,统一终端编码
  3. 一次配置,永久生效,彻底解决中文乱码问题

原文作者:字节旗下方舟 Coding Plan 原文链接:https://www.cnblogs.com/gccbuaa/p/19227315


需要我帮你把这篇文章精简成适合你自己博客发布的版本,补充更多实操细节和避坑指南吗?

评论 0

最热最新
暂无评论
余胜军说java助理Lv.1
0
影响力
0
文章
0
粉丝