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

如何在VSCode中为以//!开头的C++注释设置不同颜色?

在VSCode中区分C++的//和//!注释颜色

要实现VSCode里普通//注释显示灰色、//!注释显示绿色,按以下步骤操作:

  • 查看注释的语法作用域
    按下Ctrl+Shift+P打开命令面板,输入并选择Developer: Inspect Editor Tokens and Scopes,鼠标悬停到//!注释上,会发现它和普通//注释默认共享comment.line.double-slash.cpp这个作用域,这就是两者颜色相同的原因。

  • 自定义语法规则与颜色配置
    打开用户设置JSON文件(按下Ctrl+,后点击右上角的{}图标),添加以下配置:

    "editor.tokenColorCustomizations": {
        "textMateRules": [
            // 设定普通//注释为灰色
            {
                "scope": "comment.line.double-slash.cpp",
                "settings": {
                    "foreground": "#888888"
                }
            },
            // 设定//!注释为绿色
            {
                "scope": "comment.line.double-slash.exclamation.cpp",
                "settings": {
                    "foreground": "#00AA00"
                }
            }
        ]
    },
    "grammar-overrides": {
        "source.cpp": {
            "patterns": [
                {
                    "match": "^\\s*//\\!.*$",
                    "name": "comment.line.double-slash.exclamation.cpp"
                }
            ]
        }
    }
    
  • 重启VSCode生效
    保存配置后重启VSCode,打开你的C++文件,就能看到两种注释的颜色区分效果了。

如果上述配置不生效,可尝试安装Better C++ Syntax扩展——它对C++语法的解析更细致,可能已内置特殊注释的区分规则,安装后再调整颜色配置即可。

验证用示例代码:

#include <iostream>
using namespace std;
int main(){
  // in clion, this line is grey; in vscode it is green
  //! in clion and vscode, this line is green
  cout << "Hello world!" << endl;
  return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 15:45:47