vscode终端输出乱码

前言

​ VSCode 终端本身并不是一个从头编写的终端模拟器(像 Windows Terminal 那样)。当你在 VSCode 中选择一个终端配置文件(Profile)时,它实际上是在后台启动了一个真正的 Windows 系统 shell 进程,并将其输出捕获并显示在自己的终端面板里。至于如何设置 vscode 依赖于哪一个终端

注意,并不是这个设置

o_250906073342_image-20250906150131904.png (1000×249)

而是下面这个设置,默认是 null ,而这种情况下,vscode 默认选择第一个 PowerShell 作为终端。

o_250906073342_image-20250906150102075.png (797×552)

所以你会看到

o_250906073342_image-20250906150335215.png (627×51)

ps 的前缀代表的意思就是 powershell

这在右上角也可以看到这个信息。

o_250906073342_image-20250906150506334.png (1935×521)

选择 command Prompt ,你则会看到没有 ps 的前缀。

o_250906073342_image-20250906150726780.png (601×88)

右上角也是 cmd 的提示。

o_250906073342_image-20250906150807807.png (793×262)

不同依赖修改编码格式

如果在默认的 powershell 终端下

使用 $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 可以暂时将本页修改为 UTF-8 格式

使用 $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(936) 可以暂时将本页修改为 GBK 格式

cmd 终端下

使用 chcp 65001chcp 936 可以分别将终端编码修改为 UTF-8GBK

这样之后只要你的终端与代码文件编码相同,控制台就不会输出乱码。

posted @ 2025-09-06 15:25  wsttask  阅读(114)  评论(0)    收藏  举报