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 初始化流程中。
相关推荐
阿阿阿阿里郎13 小时前
Vscode+STM32CubeMX+Cmake联合开发教
ide·vscode·编辑器
77wpa14 小时前
VS Code(Visual Studio Code)开发调试 C/C++ 工程配置
c++·vscode
vortex514 小时前
为什么我把VSCode换成了Code-Server
ide·vscode·编辑器
0和1的舞者15 小时前
《Maven 核心功能与仓库体系详解》
ide·maven·项目管理·仓库·依赖
AI_567815 小时前
从“插件装一堆”到“效率翻一倍”——IntelliJ IDEA的插件化开发革命
java·ide·intellij-idea
前讯焦点16 小时前
星图云开发者平台页面编辑器:提供系统全面的解决方案
编辑器
保持低旋律节奏16 小时前
linux——vim编辑器
linux·编辑器·vim
那些免费的砖17 小时前
Isle-Editor (岛屿编辑器) - 免费开源 Web 富文本编辑器,也支持 Notion 块编辑、MarkDown 语法,官方支持 Vue3 开箱即用
前端·编辑器·notion
꒰ঌ小武໒꒱17 小时前
Trae CN IDE 使用教程
前端·python·编辑器
khatung17 小时前
借助Electron打通平台与用户通知(macOS系统)
前端·javascript·vscode·react.js·macos·electron·前端框架