下面是分别使用 Python 和 Shell(Bash)脚本 自动生成 .vscode
文件夹及其三个核心配置文件(settings.json
、tasks.json
、launch.json
)的完整示例。
你可以选择你熟悉的语言版本来使用,非常适合自动化项目初始化流程。
✅ 自动化目标
生成以下结构:
.vscode/
├── settings.json
├── tasks.json
└── launch.json
适用于 C++ / Qt 项目,基于 VSCode + CMake + Ninja + MinGW/GCC 环境。
🐍 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()
💡 使用方法:
- 将上述代码保存为
generate_vscode.py
- 在你的项目根目录下运行:
python generate_vscode.py
🐚 Shell(Bash)脚本版(适用于 Linux/macOS/WSL)
#!/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 配置已生成在当前目录"
💡 使用方法:
- 将上面内容保存为
generate_vscode.sh
- 赋予执行权限并运行:
chmod +x generate_vscode.sh ./generate_vscode.sh
📝 注意事项
- 如果你用的是 Windows 并且使用 CMD 或 PowerShell,建议用 Python 版;
miDebuggerPath
需要根据你本地的 GDB 安装路径修改;- 如果你使用 MSVC 编译器,需要将
launch.json
中的调试器类型改为Windows Debugger
; - 你可以将这个脚本集成到项目模板中,或添加到 CI/CD 初始化流程中。