You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code集成终端出现异常ANSI转义代码,如何修复?

VS Code集成终端显示ANSI转义代码问题修复

问题描述

我的Visual Studio Code集成终端在每条命令行前都会显示未解析的ANSI转义代码:

\x1b7\x1b[38\x3b2\x3b122\x3b122\x3b122m ❮ \x1b[0m\x1b[38\x3b2\x3b159\x3b211\x3b86m

当前VS Code终端相关设置:

"terminal.integrated.fontSize": 12,
"terminal.integrated.cursorStyle": "line",
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.enableImages": true,
"terminal.integrated.persistentSessionReviveProcess": "never"

已通过PowerShell用户配置文件配置Oh My Posh 19.24.2,配置代码:

oh-my-posh init pwsh --config "C:\Users\sergi\AppData\Local\Programs\oh-my-posh\themes\slimfat.omp.json" | Invoke-Expression

修复方案

  • 排查主题配置问题:打开slimfat.omp.json主题文件,检查是否存在格式错误的ANSI转义序列。Oh My Posh 19.x版本对转义序列的解析逻辑有调整,部分旧主题可能存在兼容问题。可以先替换为官方默认主题(如powerlevel10k_rainbow.omp.json)测试,看是否仍出现乱码。
  • 升级Oh My Posh版本:旧版本可能存在终端渲染兼容bug,执行以下命令升级到最新版,重启VS Code终端验证:
    oh-my-posh upgrade
    
  • 确认VS Code终端ANSI支持:添加或确认以下VS Code设置,确保终端开启ANSI转义序列解析:
    "terminal.integrated.enableAnsiEscapeSequences": true,
    "terminal.integrated.profiles.windows": {
      "PowerShell": {
        "source": "PowerShell",
        "args": ["-NoExit", "-Command", "$ENV:POSH_SESSION_CONFIG = 'C:\\Users\\sergi\\AppData\\Local\\Programs\\oh-my-posh\\themes\\slimfat.omp.json'; oh-my-posh init pwsh | Invoke-Expression"]
      }
    }
    
  • 重置PowerShell配置排查:暂时注释掉PowerShell配置文件中的Oh My Posh初始化代码,重启终端。如果终端恢复正常,再逐步检查主题配置中的自定义内容,排查是否有未正确编码的转义字符。
  • 更换兼容字体:确保终端使用的字体支持Oh My Posh的特殊字符(如示例中的❮),推荐使用官方推荐的MesloLGS NF字体,在VS Code中设置:
    "terminal.integrated.fontFamily": "MesloLGS NF"
    

内容的提问来源于stack exchange,提问作者Sergio Jr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:12:07