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 初始化流程中。
相关推荐
专注VB编程开发20年3 小时前
javascript的类,ES6模块写法在VSCODE中智能提示
开发语言·javascript·vscode
MediaTea7 小时前
Python 编辑器:Thonny
编辑器
笑醉踏歌行17 小时前
idea应用代码配色网站
java·ide·intellij-idea
山顶望月川18 小时前
并行科技MaaS平台支持文心4.5系列开源模型调用
人工智能·机器学习·编辑器
韩初心19 小时前
使用 visual studio 2022 编译 Lua5.4.8
ide·visual studio·lua5.4
GDAL19 小时前
Node.js REPL 教程
node.js·编辑器·vim
myloveasuka19 小时前
信号操作集函数
linux·运维·服务器·c语言·c++·vscode
向宇it20 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
comeilmforever21 小时前
IDEA2025 Version Control 窗口 local changes显示
java·ide·intellij-idea
kiss strong1 天前
好用的自带AI功能的国产IDE
ide