“comments”: “#ff0000” 配置基本无效,因现代主题均采用 textmaterules + 精确 scope 覆盖,需通过 developer: inspect editor tokens and scopes 获取真实 scope(如 comment.line.double-slash.ts)并写入 textmaterules 才生效。

直接改 "comments": "#ff0000" 基本无效,除非你用的是极老的主题(比如 2018 年前的 Default Light)。现在所有主流主题(One Dark Pro、Nord、Monokai Pro、Default Dark+)都走 TextMate scope 路线,必须用 textMateRules + 精确 scope 才能生效。
为什么 "comments" 配置不生效
VSCode 的 editor.tokenColorCustomizations 里写 "comments": "#xxx" 是个遗留字段,仅在少数基础主题中被读取。当前绝大多数主题已声明 "_defaults": true 或完全忽略该键——你保存了、重启了、甚至重装插件,颜色照样不变。
- 它不区分语言:JS 的
//、Python 的#、HTML 的<!-- -->全部混在一起处理,实际底层 scope 完全不同 - 它无法覆盖主题内置规则:主题作者用
textMateRules写了更细粒度的匹配(如comment.line.double-slash.ts),你的泛化配置优先级更低 - JSON 格式无报错,但 VSCode 静默跳过——你看到的“没反应”,其实是被彻底无视了
怎么查注释的真实 scope
不能靠猜,必须实测。把光标停在任意一行注释上(比如 TypeScript 文件里的 // todo),然后:
- 按
Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(macOS) - 输入并运行
Developer: Inspect Editor Tokens and Scopes - 看右上角弹出面板中 “Scope” 列表最顶部那个值,例如:
comment.line.double-slash.ts、comment.line.number-sign.python、comment.block.documentation.js
这个就是你要填进配置的精确 scope。不同语言、不同注释符号、甚至不同主题,scope 都可能不同。
正确配置 textMateRules 的写法
打开 settings.json(命令面板搜 Preferences: Open Settings (JSON)),添加或合并 editor.tokenColorCustomizations 块:
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": ["comment", "comment.line", "comment.block"],
"settings": {
"foreground": "#569cd6"
}
}
]
}
-
scope可以是字符串或字符串数组;填多个 scope 表示“任一匹配即生效” -
foreground必须是十六进制色值(#rrggbb或#rgb),不支持rgb()、red、变量引用 - 改完必须执行
Developer: Reload Window(热更新经常不触发 token 重着色) - 若只想改某语言(比如只动 TS 注释),应套一层语言限定:
"[typescript]": { "editor.tokenColorCustomizations": { ... } }
第三方主题下仍不生效?常见卡点
即使配置写对了,也可能被主题锁死:
- 某些主题(如 One Dark Pro v4.0+)内部设了
"_defaults": true,直接屏蔽所有用户textMateRules - 你的 scope 太宽泛(比如只写
"comment"),结果被更具体的 theme rule(如comment.line.double-slash.ts)覆盖掉 - JSON 中多了一个逗号、引号没闭合、括号错位——VSCode 设置界面右下角虽有红点,但容易被忽略;建议粘贴到 jsonlint.com 验证
- 没在对应语言文件里 inspect scope:在 .py 文件里查出来的
comment.line.number-sign.python,不能直接拿去配 JS 文件
最稳的验证路径:先切回 VSCode 自带的 Default Dark+ 主题 → 配置生效 → 再换回你常用的主题 → 若失效,说明该主题不兼容自定义 token 色,得查它文档是否提供替代方案(比如通过 workbench.colorCustomizations 控制注释背景,而非前景)。











