vscode: "Terminal: Clear" not working as desired

Steps to Reproduce:

  1. Run a python file using command palette >Python: Run file in terminal window
  2. Terminal shows the output. Now, clear the terminal using command palette >Terminal: Clear
  3. Terminal gets clear now. Run the file again in the terminal >Python: Run file in terminal window
  4. Here is where issue shows up - the terminal still shows all the previous outputs from before the last clear command. IMO, desired should be that terminal should not show any previously cleared outputs.
  5. Attached below is a gif of the screen reproducing & visualizing this issue

Screenshot PC - 072 - 2019_06_08_1016

Does this issue occur when all extensions are disabled?: Yes, the issue reoccurs. (Note: All except the “Python” extension were disabled. Python extension is needed to run the python file in terminal.

I had also posted in Super User forum thinking this was some issue with my system. And then I tried it on a clean build of VS code and it got repeated. Hence, I decided to post it as a bug.

Link to Super User forum question

VS Code version: Code - Insiders 1.36.0-insider (c089daa858f34631f5f827ff8a0313bf1e2ded6d, 2019-06-07T05:19:15.188Z) OS version: Windows_NT x64 10.0.18362

System Info
Item Value
CPUs Intel® Core™ i7-8700K CPU @ 3.70GHz (12 x 3696)
GPU Status 2d_canvas: enabled
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: disabled_software
oop_rasterization: disabled_off
protected_video_decode: enabled
rasterization: enabled
skia_deferred_display_list: disabled_off
skia_renderer: disabled_off
surface_synchronization: enabled_on
video_decode: enabled
viz_display_compositor: disabled_off
webgl: enabled
webgl2: enabled
Load (avg) undefined
Memory (System) 15.68GB (7.71GB free)
Process Argv
Screen Reader no
VM 0%
Extensions (1)
Extension Author (truncated) Version
python ms- 2019.5.18875

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 45
  • Comments: 83 (22 by maintainers)

Commits related to this issue

Most upvoted comments

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

It’ll be fixed by work on the ConPTY API that’ll be tracked by microsoft/terminal#1193.

@mrkarthi I suggest we don’t bump this issue anymore, because it’s already marked as an upstream problem.

But anyway, feel free to add your thumbs up to this and the upstream issue to add priority to them.

While this issue persists, I want to share what I have been using as a temporary workaround:

  1. As of VS-Code v 1.44.0, executing either “cls” or “clear terminal” command in the integrated terminal seems to clear the terminal output properly
  2. Although, “Terminal: Clear” still does NOT work properly
  3. I have created a keybinding (in my case CTRL+k) to accomplish this.
    Add the following code in your “keybindings.json” file (refer to the attached GIF for details)

{ "key": "ctrl+k", "command": "workbench.action.terminal.sendSequence", "args": { "text": "cls \u000D" }, "when": "terminalFocus" }

  1. The attached GIF shows, in order, the following:
    • “Terminal: Clear” command NOT working properly
    • “cls” and “clear terminal” commands working in integrated terminal
    • How to modify the keybindings.json file to make a keyboard shortcut
    • Verifying that keyboard shortcut works

Hope that this helps!

Screenshot PC

EDIT - Changed the code snippet Using “CTRL+k” as shortcut key and not adding a “when”: “terminalFocus” value in keybindings.json property somehow stops all other shortcuts that start with “CTRL+K” from running properly e.g. the default theme selection shortcut key (CTRL+K, CTRL+T)

So, either use a different shortcut key e.g. (CTRL+K, CTRL+K) for clearing the terminal - you can exclude “when”: “terminalFocus” from the code snippet above and this shortcut will work even when you are not directly focused in the Terminal

Or else, include the “when”: “terminalFocus” in the keybindings.json file (as is in the code snippet above) and the “CTRL+K” shortcut will run only when Terminal is in focus

confirmed

it’s a pitty that such a bug still exists, it’s f… annoying

I can confirm this issue still exists. If I run my “terminal: clear” hotkey any subsequent code will show both my old terminal + my newly run code.

@vivekmalhotra the bot closed it because of the needs more info label, reopened.

Looks like the upstream issue was fixed by adding a new clear buffer API to conpty, we’ll need to wait some time for the new conpty to get shipped to Windows.

@Yegorich555 As I noted above, you need to use both cls and Terminal: Clear together at the moment. Either one without the other isn’t enough.

I also dont like it and when I write ‘clear’ / ‘cls’ it also dosent clear properly when I scroll up it shows old content.

Please do something. Very annoying to CLEAR or CLS or whatever and still see the previous outputs after each command Thank you!

I’ve got to exact same problem with my projects after windows update 1803 ->1903 (clean install). No idea what changed, but I’ve never seen this before.

(Experimental duplicate detection) Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@Svish this is talking about the Terminal: Clear VS Code command, not the clear shell command. This is what you’re talking about https://github.com/xtermjs/xterm.js/issues/1727

@GearTheWorld the milestone of the issue which is the month in which the work is done. It’ll get released in early July.

Great to hear this is now fixed - thanks @Tyriar! Coincidentally, the issue got fixed on June 9th, exactly 4 years after it was first created. Time flies… 😊

@GearTheWorld the milestone of the issue which is the month in which the work is done. It’ll get released in early July.

If using PowerShell Remove-Item (Get-PSReadlineOption).HistorySavePath Reference: shellhacks.com/clear-history-powershell

Thank you for sharing your solution @GearTheWorld, but what you posted is a work-around. I think VS Code should be able to clear its terminal, no matter which shell is being used.

I perfectly understand. I had to give at least a work-around. Enjoy !

Well, seems the problem is with how terminal.clear was implemented. A workaround (not solution) based on this answer, might be to emulate common clear screen commands (For instance, ctrl+ l or any other shortcut being used in your actual terminal) for this purpose. When using Powershell or WSL Linux distros as the integrated terminals, adding the following snippet to keybindings.json would probably help.

    {
        "key": "ctrl+l",
        "command": "\u2303 l \u000D",
        "when": "terminalFocus" 
    },

And if using Command Prompt, then the following one:

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

At-least clear command is working for me. Command+K stopped from long time but I see something being expected post pressing Command + K. Please check the screenshot. Screenshot 2020-05-14 at 11 40 47 AM

Hello @Tyriar,

I have assigned the keyboard shortcut ‘ALT+M’ for clearing the Terminal.

{ "key": "alt+m", "command": "workbench.action.terminal.clear", "when": "terminalFocus" }

Clear the Terminal with the keyboard shortcut. Type “node index”, the cursor jumps in the Terminal.

image

Press Enter.

image

Terminal is not cleared.

Please check and let me know your comments.

Best Regards, Karthi M R.

Ctrl + K seems work for me instead of typing clear in terminal.

VS Code, on Windows, with regular cmd terminal, when i run clear, it does scroll the buffer so my cursor is at position 0,0, but that’s not what clear is supposed to do. It’s supposed to clear the terminal, i.e. empty the buffer completely so that I’m at a fresh start.

I use it frequently in the command-line when dealing with commands that spit out a lot of text or errors. Sometimes it’s difficult to see where the output from one command ends and the next begins, and in those cases it’s super handy to just run clear and empty the buffer completely, so that the only output on the screen will be from the command I’m about to run.

This doesn’t work at all in VS Code currently. Running clear simply scrolls the buffer up a bit, so it looks empty, but when I run my next command outputting a bunch of text, and then try to scroll up to find the beginning, I eventually get to old output, making it a lot harder. Only workaround is to exit the terminal and open a new one, which is a hassle and should be uneccessary.

clear should clear.

If using PowerShell

Remove-Item (Get-PSReadlineOption).HistorySavePath

Reference: https://www.shellhacks.com/clear-history-powershell/

@sp00x yes, this issue is about old cleared content reappearing.

@sp00x No need to resize. Everything is kept in the terminal and the clear only clears the visible part of the terminal. We simply need something to clear the history as well but that doesn’t exist.

Nope, not yet. We kinda want to make sure it’s stable, even in just the Terminal, before ingesting it into the OS. Once it’s in the OS itself, it’ll still be a few weeks before it reaches Insiders builds.

Yes, this may differ depending on many things, I believe the issue is still valid and will not be closed.

or making a new, more focused issue here and linking to that, of course

This means all the people following this issue would need to go follow that issue and it would lose all the up votes which feed into prioritization.

Note that Terminal: Clear no longer does what the original issue reported. It clears the terminal’s buffer, and it stays cleared even on subsequent commands (previously, it would seem to clear the buffer, but then if you ran a command, it would restore the buffer you just cleared. It no longer does that).

If you want a keyboard shortcut, open VS Code, go to filesettingskeyboard shortcuts, then search for terminal.clear and if it’s not already set to ctrl + shift + k, set it to that. From now on you can use the VS Code command ctrl-shift-k instead of typing something on the command line when you need to clear the cli buffer. And as long as you’re focused on the terminal, that shortcut will also clear the buffer when it’s busy executing something and not accepting user input. Just like how you don’t type anything on mac to clear the terminal, you use the buffer clearing keyboard shortcut.

(While this is explained quite well over on https://stackoverflow.com/a/48713754/740553, that information is worth repeating here)

The original issue here has been a solved problem for while, and it might be worth closing and locking this issue, so folks can file related, but distinct, issues as new issues instead (or upvote the already existing ones for the particular behaviour they’re looking at, like history-cycling in the terminal on windows after a clear performing incorrect cursor placement)

Related to #91574 ?

@manwithsteelnerves that happened when any extension adds a cmd+k chord keybinding. Actually that might have been fixed after the terminal.integrated.allowChords setting was added (true by default) 🤔, anyway that’s not related to this particular issue.

On Windows 10.0.18363.778 running cls in the terminal in VS Code 1.44 does not do as described in the above comment. Instead, it does the same thing as originally reported: it seemingly clears the terminal, then running any command will show (part of) the previous console output.

cls command doesn’t fix this issue at all for me!

No debounce,Push 1s Enter,it still effective,more than 1s

Terminal clear

I run npm in terminal,then end it with “Ctrl + C” , useing code clear and get result as left(I have click enter several times),useing “ctrl + shift + p” and “terminal clear” get result as right,it clear all and terminal crashed.

terminal will back to line 1 and enterable but not clear character。

用了clear命令后,terminal返回第一行并且可编辑,但是并没有清理界面字符,使用 控制中心的Terminal clear 指令后可以清理,但是terminal崩溃了

image

@zadjii-msft @Tyriar I can confirm that the issue is still prevalent in the latest insiders build. Unsure on how to re-open this issue?