How can I clear the terminal in Visual Studio Code?

Visual Studio-Code

Visual Studio-Code Problem Overview


I need to clean the contents of the terminal in Visual Studio Code.

Every time I use Maven, the output of the terminal is attached to the previous build, which is confusing me.

How do I clear the terminal window with some command or keyboard shortcut?

cls doesn't work; it only hides the text.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

Use Ctrl+K. This goes clean your console in Visual Studio Code.

Per comments, in later versions of VSCode (1.29 and above) this shortcut is missing / needs to be created manually.

  • Navigate: File > Preferences > Keyboard Shortcuts
  • search for workbench.action.terminal.clear
  • If it has no mapping or you wish to change the mapping, continue; otherwise note & use the existing mapping
  • Double click on this entry & you'll be prompted for a key binding. Hold CTRL and tap K. Ctrl + K should now be listed. Press enter to save this mapping
  • Right click the entry and select Change when expression. Type terminalFocus then press enter.
  • That's it. Now, when the terminal is in focus and you press Ctrl+K you'll get the behaviour you'd have expected to get from running clear/cls.

Solution 2 - Visual Studio-Code

Ctrl + Shift + P and select Terminal:clear

Solution 3 - Visual Studio-Code

>FOR VERSIONS AT AND ABOVE v1.32 SEE BELOW

Bindings for this command still need to be setup manually, even at v1.33.1, which I am at. The command is there under Terminal: Clear but the binding is blank. Here is how you can setup bindings in v1.32 and up.

Open up the Keyboard Shortcuts with Ctrl+K, Ctrl+S. Then click on the {} next to the Keyboard Shortcuts tab to open up the keybindings.json file.

keyboard shortcuts

After doing so, find some open space anywhere in the file, and type in the key bind below.

{
	"key": "ctrl+k",
	"command": "workbench.action.terminal.clear",
	"when": "terminalFocus"
}

>FOR VERSIONS UP TO v1.32 SEE BELOW

This is for Visual Studio Code v1.18 up to v1.32, I believe; I am on v1.29.1. Since apparently there are a whole lot of assumptions about the default bindings, here is how you set up a binding, then use it. Keep in mind this binding will completely delete all of the history in your terminal too.

Open up the Keyboard Shortcuts with Ctrl+K, Ctrl+S. Once you are in Keyboard Shortcuts, click on keybindings.json in the text that says ....open and edit keybindings.json. Then on the RIGHT side, in the keybindings.json, add this (make sure you put a comma before the first { if there are one or more bindings already):

{
	"key": "ctrl+k",
	"command": "workbench.action.terminal.clear",
	"when": "terminalFocus"
}

>FOR ALL VERSIONS

Remember, the "key": can be whatever binding you want. It doesn't HAVE to be Ctrl + K.

To use the keybinding, you must have focus in your terminal, and then do the binding.

Solution 4 - Visual Studio-Code

To clear Terminal in VS Code simply press Ctrl + Shift + P key together this will open a command palette and type command Terminal: Clear. Also you will go to View in taskbar upper left corner of vs code and open Command pallete.

This will clear the terminal easily & work for any directory you have open in your terminal. This is for Windows, also try if it works for Mac.

This is command is work in all VS code versions include latest version 1.52.1

Solution 5 - Visual Studio-Code

Go to

  1. File >Preferences >Keyboard shortcuts.
  2. Search for "Terminal: clear"
  3. By default no keyboard shortcut is assigned.
  4. Just click on the Plus (+)icon in the banner and give the preferred shortcut of your choice to clear the terminal.
  5. I prefer to use ctrl+k as that shortcut is not assigned with any command.

Solution 6 - Visual Studio-Code

2019 Update (Read in Full)


Shortcut

Mac: cmd + k

Windows: ctrl + k


TroubleShooting

If the shortcuts do not work for you, the most likely scenario is that either you or an extension you installed has added an open ended ctrl + k / cmd + k chord to another shortcut.

Open ended meaning, the shortcut does not have an explicit when clause that excludes terminal focus. There are two possible solutions here.

Solution 1:

If you added the shortcut, simply go to your keybindings.json file and add a when clause that does not include terminal focus. Example:

{
    "key": "cmd+k cmd+c",
    "command": "someCommandHere",
    "when": "editorTextFocus",
}

Solution 2:

Alternatively, you can add the workbench.action.terminal.clear command to the very bottom of keybindings.json, ensuring it takes precedence over other shortcuts. It'd be wise to add a comment so you don't forget and later place new chords below it. Example:

// Keep this keybinding at very bottom of file to ensure terminal clearing.
{
  "key": "cmd+k",
  "command": "workbench.action.terminal.clear",
  "when": "terminalFocus",
}

For additional information, check out this GitHub issue.

Solution 7 - Visual Studio-Code

For a MacBook, it might not be Cmd + K...

There's a long discussion for cases where Cmd + K wouldn't work. In my case, I made a quick fix with

> cmd+K +cmd+ K

Go to menu Preferences -> Key shortcuts -> Search ('clear'). Change it from a single K to a double K...

Solution 8 - Visual Studio-Code

Use Ctrl+K to clear the terminal.

But this is not possible in Linux Visual Studio Code.

For Linux you can do like below:

Via the command palette: ctrl+shift+p, "tclear"

To add this in shortcuts, copy paste this in keybinding file:

{
  "key": "ctrl+k",
  "command": "workbench.action.terminal.clear",
  "when": "terminalFocus"
}

Solution 9 - Visual Studio-Code

V1.33 won't accept ctrl + k to clear terminal (Windows).

You need to enable manually by adding below code in your keybiniding.json, it will override the defaults.

In your VSCode, press ctrl + shift + p, search for keyboard and click on Open Keyboard Shortcuts to open the keybindings.json file.

Note: If you open the Default one, it will show the default settings, but as this file is read-only, you can't edit it manually.

Paste the following script in keybindings.json

{
            "key": "ctr+k",
            "command": "workbench.action.terminal.clear",
            "when": "terminalFocus"
}

Save the file.

Now your terminal will clear by crtl + k

Solution 10 - Visual Studio-Code

To clear the terminal, using default keybindings on the newest version of VS-Code, you press CTRL-L.

Solution 11 - Visual Studio-Code

You can use below to clear the screen in terminal: cls; or clr

Solution 12 - Visual Studio-Code

If you're on a Mac, it's +k. :)

Solution 13 - Visual Studio-Code

By default there is NO keybinding associated to clearing the terminal in VSCode. Therefore, one must add a NEW Keybinding by following the below steps:

  1. Navigate: File --> Preferences --> Keyboard Shortcuts. (Or Ctrl + K and Ctrl + S together)
  2. Type in the Search Bar on top: Terminal: Clear.
  3. An entry with Command: Terminal:Clear will show up with the following VSCode command workbench.action.terminal.clear.
  4. Right Click on it and press Change Keybinding. Then press Ctrl + K together and then Enter. It will be saved.
  5. Right Click again and press Change When Expression. Just enter: terminalFocus. It will be saved.

> Finally, open your integrated terminal and with focus inside the terminal, press Ctrl + K.

Solution 14 - Visual Studio-Code

workbench.action.terminal.clear no longer works (at least for VS Code Insiders 1.54 on Mac)

The following is the way to now map CTRL+L yo the default console functionality.

{
    "key": "ctrl+l",
    "command": "workbench.action.terminal.sendSequence",
    "args": {"text": "\u000c"},
    "when": "terminalFocus"
}

Solution 15 - Visual Studio-Code

Right click on the terminal and select clear option (for ubuntu).

For mac just type clear

Solution 16 - Visual Studio-Code

The accepted answer should be the following which was unmultimedio's comment to one of the answers above:

> Cmd+K will work, you just need to set again in the Keyboard Shortcuts > the workbench.action.terminal.clear to Cmd+K, so it shows as Source: > User instead of Source: Default – unmultimedio Mar 12 '19 at 1:13

Solution 17 - Visual Studio-Code

  1. Just click the gear button on the left-bottom side on the VS code screen
  2. then Search for "Terminal: clear"
  3. By default no keyboard shortcut is assigned.
  4. Just double click the Terminal: Clear
  5. and give the preferred shortcut of your choice to clear the terminal.
  6. Usually ctrl+k is used as that shortcut is not assigned with any command.

FYI: This method is the same as @SuRa but is a little simpler. Btw: I use VS Code version 1.43.0

Solution 18 - Visual Studio-Code

I am using Visual Studio Code 1.52.1 on windows 10 machine.'cls' or 'Clear' doesn't clear the terminal.

just write

exit

It will close the terminal and press

ctrl+shift+`

to open new terminal.

Solution 19 - Visual Studio-Code

You can change from settings menu (at least from version 1.30.2 and above)...

On Mac, just hit Code > Preferences > Settings.

Then just search for "clear" and check Clear Previous Output.

Settings - Clear Previous Output

Solution 20 - Visual Studio-Code

The Code Runner extension has a setting "Clear previous output", which is what I need 95% of the time.

File > Preferences > Settings > (search for "output") > Code-runner: Clear previous output

The remaining few times I will disable the setting and use the "Clear output" button (top right of the output pane) to selectively clear accumulated output.

This is in Visual Studio Code 1.33.1 with Code Runner 0.9.8.

(Setting the keybinding for Ctrl+k did not work for me, presumably because some extension has defined "chords" beginning with Ctrl-k. But "Clear previous output" was actually a better option for me.)

Solution 21 - Visual Studio-Code

Select Open Keyboard Shortcuts from command palette and put following to keyboard shortcuts file:

{
    "key": "cmd+k",
    "command": "workbench.action.terminal.clear",
    "when": "terminalFocus"
}

Solution 22 - Visual Studio-Code

F1 key opens the shortcuts for me using windows 10. Then type Terminal and you see the clear option.

Solution 23 - Visual Studio-Code

Navigate in VS code: File > Preferences > Keyboard Shortcuts search for workbench.action.terminal.clear

Double click on this entry & you'll be prompted for key binding. Hold CTRL and tap L. Ctrl + L should now be listed. Press enter to save this mapping

That's it.

Solution 24 - Visual Studio-Code

In kde's konsole there is an action called "clear scrollback". It not only clears the terminal as usual, but also deletes the previous lines, so that you cannot scroll back and be confused.

I found in vs codium you can open Settings, search for terminal. Then go to Debugging, Debug - Terminal - Clear Before Reusing.

Set checkbox there, and every time you start a new debugging, the terminal scrollback history will not bother you.

Solution 25 - Visual Studio-Code

To permanently delete the previous commands, use this

  1. Set-PSReadlineOption -HistoryNoDuplicates

  2. Remove-Item (Get-PSReadlineOption).HistorySavePath

  3. Alt-f7

Solution 26 - Visual Studio-Code

paste this command -

Remove-Item (Get-PSReadlineOption).HistorySavePath

in your powershell and start new powershell and its found cleared

for further details check this link @ https://www.shellhacks.com/clear-history-powershell/

Solution 27 - Visual Studio-Code

I'm running the June 2020 update of Visual Studio Code. I was looking for a simple way to clear the history of Terminal output from a Python script. Killing the Terminal via clicking on the trash can icon on the upper right did what I needed, and my Conda environment automatically reactivated.

Solution 28 - Visual Studio-Code

Try typing in 'cls', if that doesn't work, type 'Clear' capital C. No quotes for any. Hope this helps.

Solution 29 - Visual Studio-Code

I am using Visual Studio Code 1.38.1 on windows 10 machine.

Tried the below steps:

  1. exit()

  2. PS C:\Users\username> Cls

  3. PS C:\Users\username>python

Solution 30 - Visual Studio-Code

just type 'clear' in the terminal (windows) or ctrl+shift+p and on mac - right click

Solution 31 - Visual Studio-Code

CRTL + Backspace It works also

Solution 32 - Visual Studio-Code

I just click on the trash bin icon to close it.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondaniloView Question on Stackoverflow
Solution 1 - Visual Studio-CodeJeterson Miranda GomesView Answer on Stackoverflow
Solution 2 - Visual Studio-CodetibooView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeSovietFrontierView Answer on Stackoverflow
Solution 4 - Visual Studio-Codeakshay_sushirView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeSuRaView Answer on Stackoverflow
Solution 6 - Visual Studio-CodejabacchettaView Answer on Stackoverflow
Solution 7 - Visual Studio-CodeMartian2049View Answer on Stackoverflow
Solution 8 - Visual Studio-CodeJitendra PawarView Answer on Stackoverflow
Solution 9 - Visual Studio-CodeArslan MunirView Answer on Stackoverflow
Solution 10 - Visual Studio-Coderokus_invView Answer on Stackoverflow
Solution 11 - Visual Studio-Codeuser11951698View Answer on Stackoverflow
Solution 12 - Visual Studio-CodeClifton LabrumView Answer on Stackoverflow
Solution 13 - Visual Studio-CodeRICHARD ABRAHAMView Answer on Stackoverflow
Solution 14 - Visual Studio-CodeBrandon SedaView Answer on Stackoverflow
Solution 15 - Visual Studio-CodeAniketView Answer on Stackoverflow
Solution 16 - Visual Studio-CodeTom DoeView Answer on Stackoverflow
Solution 17 - Visual Studio-CodeAdeel Raza AzeemiView Answer on Stackoverflow
Solution 18 - Visual Studio-CodeSuvra.DView Answer on Stackoverflow
Solution 19 - Visual Studio-CodeNíkolas La PortaView Answer on Stackoverflow
Solution 20 - Visual Studio-CodeJon Olav VikView Answer on Stackoverflow
Solution 21 - Visual Studio-CodeziishanedView Answer on Stackoverflow
Solution 22 - Visual Studio-CodeRollingInTheDeepView Answer on Stackoverflow
Solution 23 - Visual Studio-CodeHemachandraView Answer on Stackoverflow
Solution 24 - Visual Studio-CodeAsharkView Answer on Stackoverflow
Solution 25 - Visual Studio-CodePremView Answer on Stackoverflow
Solution 26 - Visual Studio-CodeShivam VermaView Answer on Stackoverflow
Solution 27 - Visual Studio-CodeSteveView Answer on Stackoverflow
Solution 28 - Visual Studio-CodeiainView Answer on Stackoverflow
Solution 29 - Visual Studio-Codebash7586View Answer on Stackoverflow
Solution 30 - Visual Studio-CodeChuck E.View Answer on Stackoverflow
Solution 31 - Visual Studio-CodeAlan KmView Answer on Stackoverflow
Solution 32 - Visual Studio-CodemeolView Answer on Stackoverflow