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

如何在VS Code中修改Python的global关键字颜色?

修改VS Code中Python global关键字的颜色

要单独修改Python里global关键字的颜色,你需要在settings.json的textMateRules中添加一条针对其专属作用域的规则:

  1. 打开VS Code的设置JSON文件(快捷键Ctrl+,打开设置面板后,点击右上角的打开设置(JSON)图标)
  2. 在editor.tokenColorCustomizations的textMateRules数组中插入以下规则:
{
    "scope": "keyword.control.global.python",
    "settings": {
        "foreground": "#你的颜色码"
    }
}
  1. 替换#你的颜色码为你想要的颜色(比如#ff0000表示红色)

修改后的完整editor.tokenColorCustomizations配置示例:

{
    "editor.tokenColorCustomizations": {
        "comments": "#00af0c",
        "strings": "#c7884a",
        "variables": "#91e2ff",
        "functions": "#fff07a",
        "keywords": "#d981ff",
        "types": "#ff73d3",
        "numbers": "#beffac",
        "textMateRules": [
            {
                "scope": "keyword.operator",
                "settings": {
                    "foreground": "#786831"
                }
            },
            {
                "scope": [
                    "source.python",
                    "punctuation.definition.variable.python"
                ],
                "settings": {
                    "foreground": "#ffcaca"
                }
            },
            {
                "scope": "keyword.control.global.python",
                "settings": {
                    "foreground": "#ff0000"
                }
            }
        ]
    }
}

说明

global关键字对应的TextMate作用域是keyword.control.global.python,这个作用域比通用的keywords更具体,优先级更高,能单独覆盖它的颜色设置,解决你之前尝试其他作用域无效的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 04:48:18