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 初始化流程中。
相关推荐
zh_xuan6 小时前
Visual Studio 上传工程到github
ide·git·github·visual studio
无限进步_8 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode
无限进步_10 小时前
【C++】多重继承中的虚表布局分析:D类对象为何有两个虚表?
开发语言·c++·ide·windows·git·算法·visual studio
徐先生 @_@|||11 小时前
基于Translation插件实现在pycharm本地翻译并阅读英文资料
ide·python·pycharm
深挖派14 小时前
GoLand 2026.1 安装配置与环境搭建 (保姆级图文教程)
后端·golang·编辑器·go·goland
银河系的一束光15 小时前
使用 IntelliJ IDEA 开发 Java 程序时 , 会遇到以下中文乱码问题 :
java·ide·intellij-idea
小宋加油啊15 小时前
Mac vscode安装PCL
ide·vscode·macos
xiaotao13116 小时前
阶段零:IDE选择 与 Jupyter Notebook / Lab 使用
ide·人工智能·python·jupyter
꯭爿꯭巎꯭17 小时前
visual studio code (vscode)下载
ide·vscode·编辑器
范什么特西17 小时前
MyEclipse8.5配置
java·ide·myeclipse