简体   繁体   English

如何在 VS Code 中运行每个代码后自动清除终端(无 Code Runner,无需修改 Java 文件)

[英]How to automatically clear the terminal after every code run in VS Code (No Code Runner, No modifying a Java file)

I'm interested in being able to automatically clear the terminal every time I run my code (Java program with a main method that may or may not have packages inside the file).我有兴趣能够在每次运行代码自动清除终端(Java 程序的 main 方法可能在文件中包含也可能没有包)。 Code Runner only runs single class Java files and doesn't work with Java files that have packages so I'm not interested in that. Code Runner 仅运行单个 class Java 文件,并且不适用于包含软件包的 Java 文件,因此我对此不感兴趣。

At the momement I'm running all my Java files by clicking on the "Run and Debug" button in VS Code.目前,我通过单击 VS Code 中的“运行和调试”按钮来运行我的所有 Java 文件。 I'm aware that I can manually clear the console using Command + K (Mac) but doing this for every run feels repetitive so I'm looking for a way to do this automatically.我知道我可以使用 Command + K (Mac) 手动清除控制台,但每次运行都执行此操作感觉重复,因此我正在寻找一种自动执行此操作的方法。 Every time I click that run and debug button I'd like for it to clear the console before running an code .每次单击该运行和调试按钮时,我都希望它在运行代码之前清除控制台 Is there also anyway to show a run button that maps to VS Code's "Run and Debug Button (Similar to the way Code Runner does it by displaying a run/triangle icon (left a screenshot below). Currently I have to make two clicks in order to click "Run and Debug".是否还显示一个映射到 VS Code 的“运行和调试按钮”的运行按钮(类似于 Code Runner 通过显示运行/三角形图标来实现它的方式(在下面留下屏幕截图)。目前我必须点击两次为了单击“运行和调试”。

I'm only looking for a solution that doesn't involve changing any Java files so something that involves changing settings/using an extension/related on VS code.只是在寻找一种不涉及更改任何 Java 文件的解决方案,因此涉及更改设置/使用扩展名/与 VS 代码相关的内容。 I'm not looking to modify any Java file.我不想修改任何 Java 文件。 Code Runner has the functionality I desire (clear terminal automatically) but doesn't work with all types of Java files so it again can't be used. Code Runner 具有我想要的功能(自动清除终端),但不适用于所有类型的 Java 文件,因此它再次无法使用。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

You could just put this line in your java file to clear the terminal.您可以将这一行放入您的 java 文件中以清除终端。

For Windows :对于Windows

   Runtime.getRuntime().exec("cls");

For Linux :对于Linux

   Runtime.getRuntime().exec("clear");

If you can modify your output you can print an ANSI clear screen escape sequence before printing your first line of output.如果您可以修改 output,则可以在打印第一行 output 之前打印 ANSI 清除屏幕转义序列。

System.out.println("\033[2J\033[1;1H");

Note, this will only work with VTE based terminals like Terminal.app, iTerm, almost all linux terminal emulators, Windows Powershell etc.请注意,这仅适用于基于 VTE 的终端,如 Terminal.app、iTerm、几乎所有 linux 终端仿真器、Windows Powershell 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM