应直接修改 editor.tokenColorCustomizations 中的 textMateRules,通过 Developer: Inspect Editor Tokens and Scopes 获取准确 TextMate scope,注意语义高亮(semanticTokens)优先级更高,需在 editor.semanticTokenColorCustomizations 中配置规则。

直接改 editor.tokenColorCustomizations,别碰主题文件,也别去动 workbench.colorCustomizations——后者只管侧边栏、状态栏这些 UI 元素,对代码文本完全无效。
怎么精准定位想改的语法元素 scope
VSCode 不认“字符串”“函数名”这种中文描述,只认 TextMate scope(比如 string.quoted.double.tsx 或 keyword.control.js)。随便写 "strings": "#ff0000" 大概率没反应。
- 把光标停在目标代码上(例如一个双引号包裹的字符串)
- 按
Ctrl+Shift+P(Win/Linux)或Cmd+Shift+P(Mac),运行Developer: Inspect Editor Tokens and Scopes - 看弹出面板里显示的
scope字段值,通常是一长串带点的名称,取最前面通用的部分即可(如只用string就能匹配所有字符串,不必写全string.quoted.single.python) - 注意语言上下文:同一段代码在 .ts 和 .tsx 文件里 scope 可能不同,
Inspect Editor Tokens显示的 scope 是当前文件的真实值
在 settings.json 里加 textMateRules 规则
打开设置 → 右上角「Open Settings (JSON)」→ 在顶层对象里加 editor.tokenColorCustomizations 块。重点不是“加颜色”,而是结构不能错:
-
textMateRules必须是数组,每项含scope和settings -
scope填你从 Inspect 工具里抄来的值(支持前缀匹配,如comment覆盖所有注释) -
settings.foreground只接受#rrggbb或rgba(r,g,b,a),不支持red这种命名色 - 如果只想改某语言(比如只让 Python 的数字变蓝),用 language override:
"[python]"作为外层 key,里面再嵌editor.tokenColorCustomizations
示例(全局改所有关键字为深红加粗):
{
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "keyword",
"settings": {
"foreground": "#c7254e",
"fontStyle": "bold"
}
}
]
}
}
语义高亮(semanticTokens)优先级更高,容易覆盖词法规则
如果你启用了 "editor.semanticHighlighting": true(默认开启),那 editor.semanticTokenColorCustomizations 的规则会压过 editor.tokenColorCustomizations。很多语言服务器(如 Volar、Intelephense)提供的变量/函数/类型识别,走的就是这条路。
- 想改函数调用颜色,先确认它是词法(
support.function)还是语义(function.call)——Inspect 工具里会明确标出 “Semantic token type” - 语义规则必须写在
editor.semanticTokenColorCustomizations下,且结构稍不同:用rules字段,key 是语义 token 名(如"function.declaration"),value 是颜色 - 语义规则不支持
fontStyle,只支持foreground和background - 如果词法和语义规则同时存在且作用于同一处,语义赢;但语义规则缺失的部分,仍会回落到词法规则
真正难的不是加几行配置,而是 Inspect 工具里看到的 scope 常常嵌套多层、语言间差异大,而且一旦启用语义高亮,你就得同时盯两套规则系统。很多人调了半天发现没生效,八成是 scope 写错了,或者语义规则悄悄覆盖了你的词法设置。


















