335

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.

SovietFrontier
  • 2,047
  • 1
  • 15
  • 33
danilo
  • 7,680
  • 7
  • 43
  • 46
  • 3
    Why is there so much repetition in the answers here? Please read what others wrote before you consider adding another answer to this. – Mark Jun 09 '20 at 23:03
  • 'ctr + L' or 'clear' in console - 2022 – Vitaliy Terziev Jan 19 '22 at 15:28
  • First: That doesn't work in VS Code - you have to restore the `workbench.action.terminal.clear` keybinding in current versions. (:facepalm:) @Mark Please try to be kinder, there are dozens of ways to search for something and they don't always return good answers. I, myself, have spent days searching for something, only to discover that I was using the wrong term. – Jim JR Harris Jan 23 '22 at 13:35

33 Answers33

419

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.
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
Jeterson Miranda Gomes
  • 4,967
  • 2
  • 14
  • 22
  • 36
    also don't forget to add `"when": "terminalFocus"` to the binding otherwise other bindings would not work (e.g. `Ctrl+K+Enter`) – Pavel Agarkov Dec 01 '18 at 11:56
  • 3
    To add on to what @PavelAgarkov mentioned, you can set the "when" by clicking the "keybindings.json" link in the message at the top of the keyboard shortcuts that says, "For advanced customizations open and edit keybindings.json" – Ryan Jan 09 '19 at 20:56
  • 1
    @onieltelies keep in mind this will not delete your history, only clear your current screen. If you scroll up you'll see history. – SovietFrontier May 16 '19 at 16:51
  • 1
    Works in OSX, but not in Windows. See the fuller answer below: https://stackoverflow.com/a/53343777/418150 – Lee Goddard Jul 29 '19 at 08:53
  • I also map to clear my output channel as well as terminal, really good for when you have a long lived test runner: `⌘+Shift+P -> Open Keyboard Shortcuts -> Clear Output => Shift+⌥+⌘+K` – Ralph Cowling Nov 18 '19 at 11:24
  • 1
    This accepted answer is inadequate until it incorporates @CatBusStop's caveat. I had all the settings correct, but this feature didn't work until I removed and re-declared the `terminal.clear` action as described. – Tom Feb 01 '20 at 21:40
  • `workbench.action.terminal.clear` was missing in `keyboard shortcuts`, so I added this in `keybindings.json` { "key": "ctrl+d", "command": "workbench.action.terminal.clear" }, – JinSnow Mar 21 '20 at 08:36
  • 2
    update: the shortcut is saved, but it doesn't work anymore (it clears it but the terminal show the results of the previous command) – JinSnow Dec 11 '20 at 19:39
  • What if we want to clear output instead of terminal – Say OL Oct 24 '21 at 12:53
  • In VS Code 1.6.3.2, (maybe earlier?), you have to right-click on the line showing the keybinding and select "Change when expression" and then type the literal string "terminalFocus" in the space that opens up. – Jim JR Harris Jan 23 '22 at 13:44
  • It does not work: previous commands are still there once I run next command! Actually, seems like it never worked for me. Now I'm using the latest VS Code - 1.64.2 – Alexander Feb 11 '22 at 07:49
  • 'ctrl+K' clear console temporary, once you click on something else, the history back on. This answer didn't fix it. – Ava Sep 20 '22 at 08:47
183

Ctrl + Shift + P and select Terminal:clear

tiboo
  • 8,213
  • 6
  • 33
  • 43
  • couldn't get anything else other than this to work, thanks! – twominds Jun 09 '21 at 23:51
  • 2
    I use Windows and for some reason `Ctrl+L` doesn't clear the Python session (in the integrated terminal). Normal powershell can be cleared using `Ctrl+L` but it just doesn't work for Python. I associated a keybinding with this (I chose `Ctrl+T Ctrl+L`) and now I can also clear the Python session. Thanks for the tip. – Avneesh Mishra Jun 18 '21 at 08:36
  • 'ctrl+shift+P' clear console temporary, once you click on something else, the history back on. This answer didn't fix it. – Ava Sep 20 '22 at 08:49
110

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.

Eugene Chybisov
  • 1,634
  • 2
  • 23
  • 32
SovietFrontier
  • 2,047
  • 1
  • 15
  • 33
  • 3
    Thank you. @danilo, you might consider making this the accepted answer as it's more detailed and up to date. – idmadj Jul 07 '19 at 00:43
  • It looks like MS have changed this a bit. In v1.41.1 I couldn't find keyboard shortcuts {}. However, F1 > Preferences: Open Keyboard Shortcuts (JSON) allowed me to create the shortcut as you prescribed. – woter324 Dec 31 '19 at 01:37
  • @woter324 if you want to edit my post following the same formatting i used, please do! i do not use vsCode anymore and I am on Mac now instead of Windows. – SovietFrontier Jan 02 '20 at 16:47
55

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 command is work in all VS code versions include latest version 1.52.1

akshay_sushir
  • 1,483
  • 11
  • 9
  • 7
    This is the only correct solution that works fast in a modern version of VS code. For some reason Google knows that and shows this one directly before clicking trough stack overflow. – Roel May 08 '20 at 14:18
  • 1
    Any short cut keys to achieve this? – Ashok kumar Ganesan Aug 25 '20 at 05:29
  • It is possible to run such command as cmd? because I am using node js and I want to add this feature in package.json that every time nodejs runs it clears the terminal first – Sam Oct 23 '20 at 12:29
  • I did not find any short cut yet to clear terminal in VS Code, I have a cheat sheet of all shortcuts for VS code see this https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf – akshay_sushir Jan 11 '21 at 10:36
  • 2
    The best way is to set a custom keybinding to 'Terminal: Clear' and use it. Cheers! – Gokul Krishna Feb 27 '21 at 15:24
  • @akshay_sushir When you follow the directions above, you'll see the keyboard hotkey on the far right of `Terminal: Clear`. If you highlight the `Terminal: Clear` line, you'll also see a gear icon appear letting you set your own hotkey. The default is `ctrl+k` in Windows and `cmd+k` in macOS. – spex Dec 07 '21 at 17:40
  • "Simply" LOL??? – StayCool Mar 23 '23 at 09:57
41

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.
SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54
SuRa
  • 1,053
  • 9
  • 13
  • Note: Ctrl+K can be used for many other key combinations like Ctrl+K, Ctrl+C (comment code). At least this is the default behavior after I installed VSCode on Windows. – elshev Apr 26 '21 at 03:36
17

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.

jabacchetta
  • 45,013
  • 9
  • 63
  • 75
12

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...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MartianMartian
  • 1,753
  • 1
  • 18
  • 26
  • 4
    `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 01:13
11

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"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jitendra Pawar
  • 1,285
  • 15
  • 27
  • 1
    Just wanted to add that in VS Code 1.18, this was in the keybindings.js but I had to add it to the user keybindings.js before it would start working. Also in reading on this, it appears that adding it to the end is important but not the case for me. See: https://github.com/Microsoft/vscode/issues/36144#issuecomment-346736998 – rwg Oct 18 '18 at 17:20
  • You can only use this AFTER you bind those keys. – SovietFrontier Nov 16 '18 at 18:31
7

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

Arslan Munir
  • 417
  • 5
  • 10
  • Where do I find keybindings.json? – pabrams Aug 15 '19 at 16:35
  • @pabrams For VS code v1.37.1 This will work. You could open keyboard shortcuts file by using Ctrl + K, Ctrl + S. On the right corner, click on file swap icon, to open JSON version for keyboard shortcuts file. – Arslan Munir Aug 19 '19 at 09:18
7

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

rokus_inv
  • 113
  • 1
  • 3
5

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"
}
Brandon Seda
  • 61
  • 1
  • 1
4

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

user11951698
  • 57
  • 1
  • 2
4

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

For mac just type clear

Aniket
  • 982
  • 1
  • 11
  • 16
3

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

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
3

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.

RICHARD ABRAHAM
  • 2,218
  • 20
  • 26
  • Excellent directions, worked for me. Windows - VSCode 1.49+ - Nodejs/Reactjs/Webpack config. *Thanks!* – cssyphus Oct 12 '20 at 16:38
3

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.

2
  1. Just click the gear button on the left-bottom side on the VS code screen
  2. Click Keyboard shortcuts
  3. then Search for "Terminal: clear". By default no keyboard shortcut is assigned.
  4. Just double click the Terminal: Clear. If the shortcut is already used; press Esc and retry.
  5. and give the preferred shortcut of your choice (for clearing the terminal).

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

1

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

Tom Doe
  • 866
  • 1
  • 13
  • 22
1

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.

Suvra.D
  • 71
  • 2
  • 11
0

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

0

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.)

Jon Olav Vik
  • 1,421
  • 2
  • 12
  • 20
0

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

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

ziishaned
  • 4,944
  • 3
  • 25
  • 32
0

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

0

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.

Ashark
  • 643
  • 7
  • 16
-1

To permanently delete the previous commands, use this

  1. Set-PSReadlineOption -HistoryNoDuplicates

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

  3. Alt-f7

Prem
  • 303
  • 2
  • 9
-1

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/

-1

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.

Steve
  • 79
  • 2
-2

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

iain
  • 7
-2

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

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
-3

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

Chuck E.
  • 35
  • 3
-4

CRTL + Backspace It works also

Alan Km
  • 19
  • 1
  • 7
-6

Push that garbage can:

enter image description here

when only one run:

enter image description here

The terminal reappears, when use it, so killing is no problem.

Narutofan
  • 9
  • 6
-8

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

enter image description here

meol
  • 1,027
  • 11
  • 7