如何在Visual Studio Code运行代码前自动清除终端内容?
Fixing VS Code Terminal Auto-Clear for Code Runs
Hey there! I totally get how annoying it is to dig through old terminal output every time you run your code—PyCharm’s auto-clear feature sets a high bar, right? Let’s get this sorted for your VS Code Education Edition.
First, let’s break down solutions based on how you’re running your code:
If you’re using the Code Runner extension (the common "Run Code" button in the top right):
- Open the Command Palette by pressing
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(Mac). - Type
Preferences: Open Settings (JSON)and hit enter—this opens the raw settings file, which lets us bypass any UI search gaps. - Add this line inside the curly braces of the JSON object (make sure commas are correctly placed if you have existing settings):
"code-runner.clearPreviousOutput": true - Save the file, and next time you click "Run Code", the terminal will clear old output before running your new code.
- Open the Command Palette by pressing
If you’re using VS Code’s built-in Python run/debug tools instead of Code Runner:
- Open the Command Palette again and select
Preferences: Open Settings (UI). - Search for Terminal: Clear Before Executing and check the box. This will make the terminal auto-clear every time you start a run or debug session.
- Open the Command Palette again and select
A quick heads-up: Sometimes UI setting searches can miss options due to extension versions or edition-specific tweaks, so going straight to the JSON settings is a reliable workaround.
内容的提问来源于stack exchange,提问作者Heikki
相关产品推荐
相关产品推荐

