VSCode 工作区配置文件通用模板创建脚本

下面是分别使用 PythonShell(Bash)脚本 自动生成 .vscode 文件夹及其三个核心配置文件(settings.jsontasks.jsonlaunch.json)的完整示例。

你可以选择你熟悉的语言版本来使用,非常适合自动化项目初始化流程。


✅ 自动化目标

生成以下结构:

复制代码
.vscode/
├── settings.json
├── tasks.json
└── launch.json

适用于 C++ / Qt 项目,基于 VSCode + CMake + Ninja + MinGW/GCC 环境。


🐍 Python 脚本版

python 复制代码
import os
import json

VS_CODE_DIR = ".vscode"

CONFIGS = {
    "settings.json": {
        "cmake.generator": "Ninja",
        "cmake.configureOnOpen": True,
        "cmake.buildDirectory": "${workspaceFolder}/build",
        "cmake.clearOutputBeforeBuild": True,
        "cmake.useCmakeListsTxt": True,
        "files.exclude": {
            "**/.git": True,
            "**/.DS_Store": True,
            "**/__pycache__": True
        },
        "editor.tabSize": 4,
        "editor.formatOnSave": True
    },
    "tasks.json": {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "CMake: Configure",
                "type": "shell",
                "command": "cmake",
                "args": ["-B", "${workspaceFolder}/build", "-G", "Ninja"],
                "group": {"kind": "build", "isDefault": True},
                "problemMatcher": ["$cmake"]
            },
            {
                "label": "CMake: Build",
                "type": "shell",
                "command": "cmake",
                "args": ["--build", "${workspaceFolder}/build"],
                "group": {"kind": "build", "isDefault": True},
                "problemMatcher": ["$cmake"]
            },
            {
                "label": "CMake: Clean",
                "type": "shell",
                "command": "rm -rf build/*"
            }
        ]
    },
    "launch.json": {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "GDB Debug",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/myapp.exe",
                "args": [],
                "stopAtEntry": False,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": True,
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe"
            }
        ]
    }
}

def create_vscode_config():
    if not os.path.exists(VS_CODE_DIR):
        os.makedirs(VS_CODE_DIR)

    for filename, content in CONFIGS.items():
        path = os.path.join(VS_CODE_DIR, filename)
        with open(path, 'w', encoding='utf-8') as f:
            json.dump(content, f, indent=4)
        print(f"✅ 已创建 {path}")

if __name__ == "__main__":
    create_vscode_config()

💡 使用方法:

  1. 将上述代码保存为 generate_vscode.py

  2. 在你的项目根目录下运行:

    bash 复制代码
    python generate_vscode.py

🐚 Shell(Bash)脚本版(适用于 Linux/macOS/WSL)

bash 复制代码
#!/bin/bash

VS_CODE_DIR=".vscode"

mkdir -p "$VS_CODE_DIR"

# settings.json
cat > "$VS_CODE_DIR/settings.json" << EOL
{
  "cmake.generator": "Ninja",
  "cmake.configureOnOpen": true,
  "cmake.buildDirectory": "\${workspaceFolder}/build",
  "cmake.clearOutputBeforeBuild": true,
  "cmake.useCmakeListsTxt": true,
  "files.exclude": {
    "**/.git": true,
    "**/.DS_Store": true,
    "**/__pycache__": true
  },
  "editor.tabSize": 4,
  "editor.formatOnSave": true
}
EOL

# tasks.json
cat > "$VS_CODE_DIR/tasks.json" << EOL
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "CMake: Configure",
      "type": "shell",
      "command": "cmake",
      "args": ["-B", "\${workspaceFolder}/build", "-G", "Ninja"],
      "group": { "kind": "build", "isDefault": true },
      "problemMatcher": ["\$cmake"]
    },
    {
      "label": "CMake: Build",
      "type": "shell",
      "command": "cmake",
      "args": ["--build", "\${workspaceFolder}/build"],
      "group": { "kind": "build", "isDefault": true },
      "problemMatcher": ["\$cmake"]
    },
    {
      "label": "CMake: Clean",
      "type": "shell",
      "command": "rm -rf build/*"
    }
  ]
}
EOL

# launch.json
cat > "$VS_CODE_DIR/launch.json" << EOL
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "GDB Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "\${workspaceFolder}/build/myapp.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "\${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\\\msys64\\\\mingw64\\\\bin\\\\gdb.exe"
    }
  ]
}
EOL

echo "✅ .vscode 配置已生成在当前目录"

💡 使用方法:

  1. 将上面内容保存为 generate_vscode.sh

  2. 赋予执行权限并运行:

    bash 复制代码
    chmod +x generate_vscode.sh
    ./generate_vscode.sh

📝 注意事项

  • 如果你用的是 Windows 并且使用 CMD 或 PowerShell,建议用 Python 版;
  • miDebuggerPath 需要根据你本地的 GDB 安装路径修改;
  • 如果你使用 MSVC 编译器,需要将 launch.json 中的调试器类型改为 Windows Debugger
  • 你可以将这个脚本集成到项目模板中,或添加到 CI/CD 初始化流程中。
相关推荐
2501_915918419 小时前
iOS编程用什么软件好?主流工具Xcode、AppCode、CodeRunner与新兴快蝎对比
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
__pop_15 小时前
VSCode Remote SSH 远程连接 glibc 高版本依赖问题
vscode
daxiangxm20 小时前
【趣味休息】“围住小猫在线玩”-“困住小猫”,又回来了
数据结构·人工智能·vscode·github·php
星光开发者21 小时前
基于Spring Boot的充电桩管理系统的设计与实现-计算机毕设【课程设计】57105
vue.js·spring boot·vscode·mysql·django·php·express
程序员允诺1 天前
内网/无网环境必备:VS Code Remote-SSH 离线包下载与手动部署教程
vscode
滕州市燕猫虎计算机科技工作室个体工商户1 天前
IDEA:Command line is too long
java·ide·intellij-idea
darin_ฅ( ̳• ◡ • ̳)ฅ12 天前
【开发工具】2026版-VScode&IDEA&Cursor插件安装及插件配置迁移
vscode·idea
别动我齐刘海2 天前
ROS2 Jazzy + C++ 实战路线——进阶学习3
c++·人工智能·vscode·python·算法·机器学习·机器人
一木 之林2 天前
手搓家庭服务器:二手装机、Docker部署、端口映射与frp内网穿透
服务器·人工智能·git·编辑器
xieliyu.2 天前
前端基础:常见CSS选择器用法
前端·css·笔记·vscode