如何在VS Code中自动启动两个Python文件并分屏显示终端?
问题:自动配置launch.json运行两个Python文件并让终端并排显示
我想配置launch.json来自动运行两个Python文件,同时让它们的终端并排显示。手动操作很简单,但自动化实现不了。目前找到最接近的方法是用命令面板里的workbench.action.terminal.join命令合并终端,但执行这个命令时需要选择要合并的目标终端,不知道怎么通过任务自动完成这个选择。
以下是我使用的launch.json配置:
{ "version": "0.2.0", "compounds": [ { "name": "Run: Server and Client", "configurations": ["Run: Server", "Run: Client"], "postDebugTask": "Join Terminals", "presentation": { "group": "server-client", "order": 1 } } ], "configurations": [ { "name": "Run: Server", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/server.py", "console": "integratedTerminal", "presentation": { "group": "server-client", "order": 1 }, "consoleTitle": "Server" }, { "name": "Run: Client", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/client.py", "console": "integratedTerminal", "presentation": { "group": "server-client", "order": 2 }, "consoleTitle": "Client" } ], }
我尝试过的简单tasks.json配置:
{ "version": "2.0.0", "tasks": [ { "label": "Join Terminals", "type": "shell", "command": ["${command:workbench.action.terminal.join}"], "problemMatcher": [] } ] }
还试过带输入命令的tasks.json配置,也没用:
{ "version": "2.0.0", "tasks": [ { "label": "Join Terminals", "type": "shell", "command": "echo ${input:terminalName}", "problemMatcher": [] } ], "inputs": [ { "id": "terminalName", "type": "command", "command": "command:workbench.action.terminal.join", "args": { "useFirstResult": true } } ] }
解决方案
核心问题是workbench.action.terminal.join本身需要交互式选择终端,没法直接在任务里自动指定。可以换两种思路实现终端并排:
思路1:用终端分组+自定义布局(无需额外扩展)
VS Code支持通过终端分组和布局参数直接实现并排,不需要合并终端。修改launch.json的presentation字段,给每个配置添加panel选项:
{ "version": "0.2.0", "compounds": [ { "name": "Run: Server and Client", "configurations": ["Run: Server", "Run: Client"], "presentation": { "group": "server-client", "order": 1 } } ], "configurations": [ { "name": "Run: Server", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/server.py", "console": "integratedTerminal", "presentation": { "group": "server-client", "order": 1, "panel": "new" // 启动新终端面板 }, "consoleTitle": "Server" }, { "name": "Run: Client", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/client.py", "console": "integratedTerminal", "presentation": { "group": "server-client", "order": 2, "panel": "split" // 拆分当前面板,与Server终端并排 }, "consoleTitle": "Client" } ] }
启动复合配置时,Server会打开新终端,Client会直接拆分面板和Server终端并排,无需额外任务。
思路2:用扩展实现自动合并终端
如果一定要用合并终端的方式,可借助multi-command扩展实现自动选择终端的逻辑:
- 安装
multi-command扩展 - 在settings.json中添加自定义命令:
"multiCommand.commands": [ { "command": "multiCommand.joinServerClientTerminals", "sequence": [ // 先选中名为Server的终端 { "command": "workbench.action.terminal.selectNamed", "args": { "name": "Server" } }, // 执行合并命令,自动将其他终端合并到当前选中的Server终端 "workbench.action.terminal.join" ] } ]
- 修改tasks.json调用该自定义命令:
{ "version": "2.0.0", "tasks": [ { "label": "Join Terminals", "type": "shell", "command": "${command:multiCommand.joinServerClientTerminals}", "problemMatcher": [] } ] }
启动复合配置后,postDebugTask会自动完成终端选中与合并,实现并排显示。
内容的提问来源于stack exchange,提问作者Andrew Robb
相关产品推荐
相关产品推荐

