You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VSCode中每次运行代码时清空终端输出?尝试Code Runner遇问题

解决VSCode中Code Runner运行代码前清空终端的问题

方法1:通过Code Runner可视化设置配置

  • 打开VSCode设置(快捷键:Windows/Linux按Ctrl+,,macOS按Cmd+,)
  • 在搜索框输入Code Runner: Clear Previous Output,勾选该选项
  • 同时确保Code Runner: Run In Terminal处于勾选状态(只有在终端运行时,该清空设置才会作用于终端面板)

方法2:直接修改settings.json配置

如果可视化设置不生效,手动编辑配置文件:

  • 打开命令面板(Ctrl+Shift+P/Cmd+Shift+P),输入Open Settings (JSON)并回车
  • 在配置中添加或修改以下内容:
    "code-runner.clearPreviousOutput": true,
    "code-runner.runInTerminal": true
    
  • 针对特定语言,可手动在执行命令前添加终端清空指令:
    • Windows系统(以Python为例):
      "code-runner.executorMap": {
          "python": "cls && python -u"
      }
      
    • macOS/Linux系统(以Python为例):
      "code-runner.executorMap": {
          "python": "clear && python -u"
      }
      

方法3:用VSCode任务(tasks.json)实现

如果Code Runner的配置始终有问题,可以改用VSCode自带任务:

  • 打开命令面板,输入Tasks: Create tasks.json file from template,选择Others创建空白模板
  • 将以下内容替换进去(根据系统调整清空命令):
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Run Current File",
                "type": "shell",
                // Windows用cls,macOS/Linux用clear
                "command": "cls && python ${file}",
                "presentation": {
                    "clear": true
                },
                "problemMatcher": []
            }
        ]
    }
    
  • 按Ctrl+Shift+B选择创建好的任务运行,每次执行前都会自动清空终端

内容的提问来源于stack exchange,提问作者Huy Pham Gia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 02:05:24