# VS Code + AutoHotkey + Multi Command 调试切换方案,gpt对话的关键节点整理总结


## 一、核心问题范围

本次对话聚焦以下内容:

1. VS Code Debug 调试状态下无法直接切换 Python 文件  
2. 如何实现“一键停止当前 Debug 并启动当前文件 Debug”  
3. 使用 Multi Command 实现 VS Code 调试自动化  
4. AutoHotkey 快捷键改造(Shift+F5 → Ctrl+Alt+F5 + Debug控制)  
5. Debug 重启快捷键与命令映射关系  
6. keybindings.json 如何打开与配置

---

## 二、关键需求拆解

目标行为:

- 当前正在调试 `A.py`
- 重新启动当前 `A.py` 调试
- 按一个快捷键
- 自动执行:
  1. 停止当前 Debug
  2. 重新启动 Debug(当前文件)

---

## 三、VS Code 解决方案(Multi Command)

### 1. 核心工具

使用 VS Code 插件:

- Multi Command

用途:
- 将多个 VS Code 命令串联执行
- 实现“停止 + 启动 Debug”的组合操作

---

### 2. 推荐实现方式

#### (1)keybindings.json 配置

```json
[
    {
        "key": "ctrl+alt+f5",
        "command": "multiCommand.restartDebug"
    }
]

(2)keybindings.json 如何打开

方法一(推荐):

Ctrl + Shift + P

输入:

Open Keyboard Shortcuts (JSON)

方法二:

Ctrl + K  →  Ctrl + S

右上角点击:

打开键盘快捷键(JSON)

方法三(文件路径):

%APPDATA%\Code\User\keybindings.json

(3)settings.json 配置 Multi Command
{
    "multiCommand.commands": [
        {
            "command": "multiCommand.restartDebug",
            "sequence": [
                "workbench.action.debug.stop",
                "workbench.action.debug.start"
            ]
        }
    ]
}

3. 作用说明

  • Debug 始终作用于当前打开文件
  • 实现 Stop + Start 原子化操作

四、VS Code 内置替代方式

Stop + Start 手动方式

  • Shift + F5:停止 Debug
  • F5:启动 Debug

Restart Debug(推荐备用)

快捷键:

Ctrl + Shift + F5

效果:

  • 停止并重新启动当前 Debug 会话

五、AutoHotkey 调试快捷键改造

1. 原始脚本

!VK70::
    ; debug模式运行
    Send, ^{F5}
Return

2. 修改目标

统一快捷键:

Ctrl + Alt + F5

3. 修改后脚本(推荐)

^!F5::
    ; 重启 debug 模式
    Send, ^!{F5}
Return

4. 符号说明

  • ^ = Ctrl
  • ! = Alt
  • + = Shift

六、完整调试链路设计

VS Code 侧

  • launch.json 使用 ${file}
  • Multi Command 实现 stop + start
  • 或使用 Ctrl + Shift + F5 restart

AHK 侧

  • 统一绑定 Ctrl + Alt + F5
  • 触发 VS Code Debug 重启

七、已解决问题总结

问题1:无法在不同 Python 文件间切换 Debug

解决方案:

  • 使用 ${file} + stop/start 或 restart

问题2:无法一键切换 Debug 流程

解决方案:

  • Multi Command 串联:
    • stop debug
    • start debug

问题3:快捷键混乱(Shift+F5 / Ctrl+F5)

解决方案:

  • 统一 Ctrl + Alt + F5
  • AHK 与 VS Code 映射一致

八、重要文件与代码

1. AutoHotkey 脚本(完整)

^!F5::
    ; 重启 debug 模式
    Send, ^!{F5}
Return

2. Multi Command 配置

{
    "multiCommand.commands": [
        {
            "command": "multiCommand.restartDebug",
            "sequence": [
                "workbench.action.debug.stop",
                "workbench.action.debug.start"
            ]
        }
    ]
}

3. keybindings.json

[
    {
        "key": "ctrl+alt+f5",
        "command": "multiCommand.restartDebug"
    }
]

4. launch.json

{
    "name": "Python: Current File",
    "type": "debugpy",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal"
}