-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cls
in PowerShell and cmd.exe doesn't fully clear the Terminal scroll-back history
#1305
Comments
So I don't believe this is something we can change about cmd.exe.
In a different world, we'd update Unfortunately, we don't live in that world. We live in the universe where cmd.exe is parked indefinitely, and we can't make any changes to it. 100% of the time we think we have a trivial change we could make to cmd.exe, it results in a nightmare of back-compat bugs (for whatever reason). So while this does have a technically feasible solution, it has a bureaucratically impossible solution. |
So if this problem originates from the console app and not Terminal, what about PowerShell? |
That's a good question. I'd imagine that they're in the same situation as cmd.exe probably. I don't know the most about powershell core, but perhaps they could take the opportunity to update their |
@zadjii-msft Also, while this applies to cmd.exe, in PowerShell, |
Here's the relevant pwsh issue: There's also a related corefx issue for However, it is not clear to me whether they're taking the new terminal into account. Of note, the corefx issue relies on the ANSI escape sequence that, if supported, is meant to clear the entire buffer:
Thus, their fix will not work in this case. You need to implement it on your end, as well. Furthermore, if you do implement it, then it will also provide an immediate workaround for users, since we can then create aliases that simply echo the above sequence (this is already common on Unix-likes, where most terminals do respect this sequence, but |
Also possibly related to #1193 |
Add me to the list that has this issue. I don't think PowerShell 5.1 or CMD.exe are going to get updated to make Windows Terminal able to do clear host. Wouldn't this tool that is still in Preview be the candidate to band-aid these also-Microsoft-owned unlikely-going-to-change apps (i.e. PowerShell 5.1 and cmd.exe .) |
(I just tested locally: Emitting CSI 3 J through conpty does work, and does clear the scrollback.) |
CSI 3 J (escape [ 3 J / Coming back to this issue @int19h @JeremyTBradshaw - if it's really impossible to solve this problem without changing cmd.exe (and having just looked at the cmd.exe |
@DHowett In the most recent update of the Terminal from the Store, it still doesn't quite do the right thing - it clears the scrollback, but it doesn't always seem to be clearing the screen. For example, try this in PowerShell: ls c:\windows; echo "$([char]27)[3J" and try scrolling up. ls c:\windows; cls" What does work is combining them! ls c:\windows; cls; echo "$([char]27)[3J" Well, almost. For some reason, it doesn't clear the last line of the scrollback. But this only happens if you do it all in one command - if you clear the screen separately, it does the right thing: ls c:\windows
cls; echo "$([char]27)[3J" After carefully reading the requisite behavior for this escape sequence, I believe this all is actually correct - Unix terminals behave the same, they don't clear the scrollback for it. However, there's one other thing. I thought that echo "$([char]27)c" On the other hand, pretty much any Unix terminal I can test handles this by clearing everything. So I think the only real bug here is the handling of this one. In the meantime, the workaround is to redefine the |
Note that the above was about PowerShell tho, where But most people probably care more about PowerShell at this point, and cmd.exe users can always define an alternate command (as a batch file), and use that. |
|
Ahh, I must have mixed it up at some point! Thank you for clarifying. Anyway, for anybody else who runs into this issue, here's a workaround recipe for your Profile.ps1: remove-alias cls
function cls {
clear-host
echo "$([char]27)[2J$([char]27)[3J"
} Side note: I'm not sure I fully understand why this needs |
I'll second @Boereck's comment for the latest release (Version 0.7.3291.0). Nothing seems to clear the scrollback for me, from |
I am thankful to have stumbled upon this issue while looking for something else as I couldn't quite figure out why prior history would come back after I would run a new command following a clear. Until now, I didn't scroll up to notice the root of the problem. I am using Version 0.7.3382.0 of Windows Terminal Preview where the published work-around has no effect. I see the same issue in VSCode 1.40.2 using the Powershell Extension v2019.11.0, however I am happy to confirm that the given work-around achieves clearing the entire window buffer.
To make this work in my Microsoft.VSCode_profile.ps1 file I had to use the following. Note that rather than overwrite the existing alias, I just used 'cl' since it wasn't an existing command on my machine.
|
Does not work. |
@mixmastamyk the actual issue tracking |
yup, this is still an issue |
@jgarza9788 The PR was just merged into the preview build today, so hopefully a release is coming up soon that actually fixes this issue. |
cls
in PowerShell and cmd.exe doesn't fully clear the historycls
in PowerShell and cmd.exe doesn't fully clear the Terminal scroll-back history
PR #5627 might suffice to fix the problem with an app compat hack within Terminal (well, within Terminal's headless private conhost.exe), without having to go back and change cmd.exe or Windows' in-box PowerShell to issue the correct VT clear screen sequences that we've discussed further up in this thread. |
## Summary of the Pull Request This PR implements a pair of shims for `cmd` and `powershell`, so that their `cls` and `Clear-Host` functions will clear the entire terminal buffer (like they do in conhost), instead of just the viewport. With the conpty viewport and buffer being the same size, there's effectively no way to know if an application is calling these API's in this way with the intention of clearing the buffer or the viewport. We absolutely have to guess. Each of these shims checks to see if the way that the API is being called exactly matches the way `cmd` or `powershell` would call these APIs. If it does, we manually write a `^[[3J` to the connected terminal, to get he Terminal to clear it's own scrollback. ~~_⚠️ If another application were trying to clear the **viewport** with an exactly similar API call, this would also cause the terminal scrollback to get cleared⚠️ _~~ * [x] Should these shims be restricted to when the process that's calling them is actually `cmd.exe` or `powershell.exe`? Can I even do this? I think we've done such a good job of isolating the client process information from the rest of the host code that I can't figure out how to do this. - YES, this can be done, and I did it. * [ ] **TODO**: _While I'm here_, should I have `DoSrvPrivateEraseAll` (the implementation for `^[[2J`, in `getset.cpp`) also manually trigger a EraseAll in the terminal in conpty mode? ## PR Checklist * [x] Closes #3126 * [x] Actually closes #1305 too, which is really the same thing, but probably deserves a callout * [x] I work here * [x] Tests added/passed * [n/a] Requires documentation to be updated ## Validation Steps Performed * ran tests * checked `cls` in the Terminal * checked `Clear-Host` in the Terminal * Checked running `powershell clear-host` from `cmd.exe`
🎉This issue was addressed in #5627, which has now been successfully released as Handy links: |
as per bot, I just updated Terminal app from store. running |
There is a way to do not clear all, but allow scroll back after |
@nelson6e65 Yes, but what are you using?
However,
|
@mixmastamyk I'm using cmd/PowerShell. But I want the Unix behavior. By using I think that more than "clearing" the text, I want to scroll all content up and have a new "clean" window, with the cursor in the first line of terminal, but keeping the previous terminal outputs. |
@nelson6e65 , I believe the terminal supports it, since it supports Unixy behavior. Sounds like it needs to be implemented in your shell. Perhaps make an alias that prints the ansi escapes to do the scroll yourself in the meantime. |
Thanks. I'll try it. |
Issue persists on integrated terminal in vs code 1.85.1
the workarounds proposed above do not work: |
I feel like this problem should have been reported already, but I couldn't find it in GitHub issue search.
Environment
Windows build number:
Microsoft Windows [Version 10.0.18362.205]
- Win10 ver. 1903 with Microsoft-internal test build for monthly quality updateWindows Terminal version: 8dd2e79 - Azure DevOps CI build for AMD64
Steps to reproduce
type C:\Windows\System32\OEMDefaultAssociations.xml
cls
Expected behavior
The visible area of the window is cleared of everything except the cmd/PS prompt, and you can no longer scroll the window view to see any part of the file you displayed. This is what happens in the old console window (conhost.exe).
Actual behavior
The visible area is cleared, but the window scrollbar remains active. When you scroll up, you see that most of the file you displayed remains; only the end of the file is erased up to a length determined by your visible window height.
The text was updated successfully, but these errors were encountered: