clear screen ansi doesn't prevent previous screen from being pushed to scrollback buffer #23198

Closed
opened 2026-01-31 08:35:07 +00:00 by claunia · 10 comments
Owner

Originally created by @ldemailly on GitHub (Apr 24, 2025).

Windows Terminal version

1.21,1.22,1.23

Windows build number

10.0.26100.3775

Other Software

none though real case is fortio brick golang ansipixels demo

Steps to reproduce

Write-Host "$([char]27)[2J$([char]27)[1;1Hone`r`nprevious`r`n$([char]27)[2J$([char]27)[1;1Htwo`r`n"

or when comparing on unix/mac

echo -e "\033[2J\033[1;1Hone\r\nprevious\r\n\033[2J\033[1;1Htwo\r\n"

Expected Behavior

nothing in scroll back

Actual Behavior

one
previous

in scroll back

Originally created by @ldemailly on GitHub (Apr 24, 2025). ### Windows Terminal version 1.21,1.22,1.23 ### Windows build number 10.0.26100.3775 ### Other Software none though real case is fortio brick golang ansipixels demo ### Steps to reproduce ```powershell Write-Host "$([char]27)[2J$([char]27)[1;1Hone`r`nprevious`r`n$([char]27)[2J$([char]27)[1;1Htwo`r`n" ``` or when comparing on unix/mac ```bash echo -e "\033[2J\033[1;1Hone\r\nprevious\r\n\033[2J\033[1;1Htwo\r\n" ``` ### Expected Behavior nothing in scroll back ### Actual Behavior ``` one previous ``` in scroll back
claunia added the Needs-TriageIssue-Bug labels 2026-01-31 08:35:07 +00:00
Author
Owner

@ldemailly commented on GitHub (Apr 24, 2025):

Initially mentioned in https://github.com/microsoft/terminal/issues/18825#issuecomment-2822778126 - cc @lhecker

@ldemailly commented on GitHub (Apr 24, 2025): Initially mentioned in https://github.com/microsoft/terminal/issues/18825#issuecomment-2822778126 - cc @lhecker
Author
Owner

@ldemailly commented on GitHub (Apr 24, 2025):

note: I'm partially wrong: ghostty doesn't push to the scroll back with the above, but apple terminal does indeed
(interestingly, apple terminal with a black full rest of the screen while windows terminal only has the 2 lines right above)

alacrity does same as windows terminal
kitty does same as ghostty

so 2 against 3 :) ? what should I do in my program to not get stuff pushed and yet not use the alternate screen?

@ldemailly commented on GitHub (Apr 24, 2025): note: I'm partially wrong: `ghostty` doesn't push to the scroll back with the above, but apple terminal does indeed (interestingly, apple terminal with a black full rest of the screen while windows terminal only has the 2 lines right above) alacrity does same as windows terminal kitty does same as ghostty so 2 against 3 :) ? what should I do in my program to not get stuff pushed and yet not use the alternate screen?
Author
Owner

@DHowett commented on GitHub (Apr 24, 2025):

So! You've stumbled onto one of the great things that terminal emulators (and their authors!) disagree on!

Some of them, like gnome-terminal (libvte), Apple Terminal, and Windows Terminal implement CSI 2 J (ED Erase in Display All) as "push the contents of the viewport into scrollback". There is some disagreement on whether it should be only the actual used lines or the whole viewport.

Others (xterm, rxvt) implement it as (I believe) "destroy the current screenful of content."

There's been some other discussion about this:
https://github.com/microsoft/terminal/issues/7894 (summary here: https://github.com/microsoft/terminal/issues/7894#issuecomment-714854915)
https://github.com/microsoft/terminal/issues/2832#issuecomment-533779605

Consensus among application authors is that they should just clear the scrollback on those terminals which require it. If you look at the output of clear, for example, on gnome-terminal: \e[H\e[2J\e[3J.

This is likely why clear also sports an argument to "try not to clear the scrollback", -x.

Image

@DHowett commented on GitHub (Apr 24, 2025): So! You've stumbled onto one of the great things that terminal emulators (and their authors!) disagree on! Some of them, like gnome-terminal (libvte), Apple Terminal, and Windows Terminal implement `CSI 2 J` (ED Erase in Display All) as "push the contents of the viewport into scrollback". There is some disagreement on whether it should be only the actual used lines or the whole viewport. Others (xterm, rxvt) implement it as (I believe) "destroy the current screenful of content." There's been some other discussion about this: https://github.com/microsoft/terminal/issues/7894 (summary here: https://github.com/microsoft/terminal/issues/7894#issuecomment-714854915) https://github.com/microsoft/terminal/issues/2832#issuecomment-533779605 Consensus among application authors is that they should just clear the scrollback on those terminals which require it. If you look at the output of `clear`, for example, on gnome-terminal: `\e[H\e[2J\e[3J`. This is likely why `clear` also sports an argument to "try not to clear the scrollback", `-x`. ![Image](https://github.com/user-attachments/assets/a00811f9-f6f7-45ec-b8d4-972b600c3756)
Author
Owner

@ldemailly commented on GitHub (Apr 24, 2025):

Thanks a lot for the detailed response and context.

But using 3J deletes the whole scrollback, that's not what I aim to do, I aim for the users to not lose their previous scrollback and yet see the output of my program when it's done and also not flood their scrollback

That works as I expect already with some terminals; what can I do to achieve ^ with windows terminal?

@ldemailly commented on GitHub (Apr 24, 2025): Thanks a lot for the detailed response and context. But using 3J _deletes_ the whole scrollback, that's not what I aim to do, I aim for the users to not lose their previous scrollback and yet see the output of my program when it's done and also not flood their scrollback That works as I expect already with some terminals; what can I do to achieve ^ with windows terminal?
Author
Owner

@DHowett commented on GitHub (Apr 24, 2025):

Hmmmm....

Maybe if you home the cursor with CSI H and then CSI 0 J to erase below?

@DHowett commented on GitHub (Apr 24, 2025): Hmmmm.... Maybe if you home the cursor with `CSI H` and then `CSI 0 J` to erase _below_?
Author
Owner

@DHowett commented on GitHub (Apr 24, 2025):

(Or if you're redrawing the whole screen, can you just home the cursor with CSI H and just... overdraw everything? No need to clear!)

@DHowett commented on GitHub (Apr 24, 2025): (Or if you're redrawing the whole screen, can you just home the cursor with `CSI H` and just... overdraw everything? No need to clear!)
Author
Owner

@ldemailly commented on GitHub (Apr 24, 2025):

Genius:

echo -e "\033[H\033[0Jprevline1\033[2;1Hprevline2"; sleep 3; echo -e "\033[H\033[0Jtwo\r\n"

appears to work!

(Or if you're redrawing the whole screen, can you just home the cursor with CSI H and just... overdraw everything? No need to clear!)

Yeah I do that when most of it is overwritten; eg

curl --compressed https://demo.fortio.org/fire

but for some case where the output is "sparse" it's less bytes to clear the screen and only write where needed, eg

curl --compressed https://demo.fortio.org/life

but if I can rely on H+0J as a replacement for 2J on all terminals, I think I'll switch to that (or rather switch to that after the first clear screen so users don't loose their current screen either)

@ldemailly commented on GitHub (Apr 24, 2025): Genius: ```sh echo -e "\033[H\033[0Jprevline1\033[2;1Hprevline2"; sleep 3; echo -e "\033[H\033[0Jtwo\r\n" ``` appears to work! > (Or if you're redrawing the whole screen, can you just home the cursor with CSI H and just... overdraw everything? No need to clear!) Yeah I do that when most of it is overwritten; eg ```bash curl --compressed https://demo.fortio.org/fire ``` but for some case where the output is "sparse" it's less bytes to clear the screen and only write where needed, eg ```bash curl --compressed https://demo.fortio.org/life ``` but if I can rely on H+0J as a replacement for 2J on all terminals, I think I'll switch to that (or rather switch to that after the first clear screen so users don't loose their current screen either)
Author
Owner

@DHowett commented on GitHub (Apr 24, 2025):

(These demos are amazing! I also really like your fps tool.)

@DHowett commented on GitHub (Apr 24, 2025): (These demos are amazing! I also really like your `fps` tool.)
Author
Owner

@ldemailly commented on GitHub (Apr 24, 2025):

Thanks a lot (and thanks for the compliments), will use that strategy in fortio/terminal#103

@ldemailly commented on GitHub (Apr 24, 2025): Thanks a lot (and thanks for the compliments), will use that strategy in [fortio/terminal#103](https://github.com/fortio/terminal/issues/103)
Author
Owner

@ldemailly commented on GitHub (Apr 25, 2025):

ps: deployed that new strategy so now the life curl doesn't push every frame into the scroll back anymore while also pushing the initial previous screen... best of both world (ditto go run fortio.org/terminal/brick@latest)

@ldemailly commented on GitHub (Apr 25, 2025): ps: deployed that new strategy so now the life curl doesn't push every frame into the scroll back anymore while also pushing the initial previous screen... best of both world (ditto `go run fortio.org/terminal/brick@latest`)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#23198