Weird behavior when clearing scrollback #20897

Open
opened 2026-01-31 07:27:08 +00:00 by claunia · 3 comments
Owner

Originally created by @araggohnxd on GitHub (Nov 26, 2023).

Windows Terminal version

1.18.2822.0

Windows build number

10.0.19045.3693

Other Software

WSL: 2.0.9.0
Kernel: 5.15.133.1-1
zsh: 5.9

I use a plugin manager called zsh4humans, and it provides some commands for keeping the prompt at the bottom of the terminal. This is the command that clears the scrollback, called z4h-clear-screen-hard-bottom:

[[ -v _z4h_tty_fd ]] || return

builtin echoti civis >&$_z4h_tty_fd
builtin print -rnu $_z4h_tty_fd -- $'\e[H\e[2J'"${(pl:$((LINES-1))::\n:)}"
builtin zle -I
builtin zle -R
builtin print -rnu $_z4h_tty_fd -- $'\e[3J'
-z4h-cursor-show

This is how the variable _z4h_tty_fd is set:

if [[ -w $TTY ]]; then
  typeset -gi _z4h_tty_fd
  sysopen -o cloexec -rwu _z4h_tty_fd -- $TTY || return
  typeset -gri _z4h_tty_fd
elif [[ -w /dev/tty ]]; then
  typeset -gi _z4h_tty_fd
  if sysopen -o cloexec -rwu _z4h_tty_fd -- /dev/tty 2>/dev/null; then
    typeset -gri _z4h_tty_fd
  else
    unset _z4h_tty_fd
  fi
fi

And this is the -z4h-cursor-show function:

function -z4h-cursor-show() {
  local cnorm=${${terminfo[cnorm]-}:/*$'\e[?25h'(|'\e'*)/$'\e[?25h'}
  [[ -t 1 ]] && print -rn -- $cnorm || print -rnu $_z4h_tty_fd -- $cnorm
}

Steps to reproduce

  • Open Windows Terminal and run WSL (tested with Ubuntu and ArchWSL);
  • Install zsh4humans;
  • Edit .zshrc:
    • Delete the line zstyle ':z4h:' start-tmux no
    • Add these lines at the end of the file:
      zstyle ':z4h:' prompt-at-bottom yes
      z4h bindkey z4h-clear-screen-hard-bottom Ctrl+L
      
  • Run exec zsh or source ~/.zshrc to apply the new changes;
  • Press Ctrl-L.

Expected Behavior

The z4h-clear-screen-hard-bottom command is meant to clear the scrollback and push the prompt to the bottom of the terminal. Here is an example of how it should work.

Actual Behavior

It seems that in Windows Terminal, for some reason, the prompt itself is also cleared:

https://github.com/microsoft/terminal/assets/31555284/ed7348a3-d94c-4f7e-be57-065b5fb66be8

I opened an issue in the official zsh4humans repository, and the owner asked me to try to reproduce this issue in other terminal emulators. I tried it with Cmder, ConEmu and Hyper, and none of them share this issue.

Does anyone know what could be causing this?

Originally created by @araggohnxd on GitHub (Nov 26, 2023). ### Windows Terminal version 1.18.2822.0 ### Windows build number 10.0.19045.3693 ### Other Software WSL: 2.0.9.0 Kernel: 5.15.133.1-1 zsh: 5.9 I use a plugin manager called zsh4humans, and it provides some commands for keeping the prompt at the bottom of the terminal. This is the command that clears the scrollback, called `z4h-clear-screen-hard-bottom`: ```sh [[ -v _z4h_tty_fd ]] || return builtin echoti civis >&$_z4h_tty_fd builtin print -rnu $_z4h_tty_fd -- $'\e[H\e[2J'"${(pl:$((LINES-1))::\n:)}" builtin zle -I builtin zle -R builtin print -rnu $_z4h_tty_fd -- $'\e[3J' -z4h-cursor-show ``` This is how the variable `_z4h_tty_fd` is set: ```sh if [[ -w $TTY ]]; then typeset -gi _z4h_tty_fd sysopen -o cloexec -rwu _z4h_tty_fd -- $TTY || return typeset -gri _z4h_tty_fd elif [[ -w /dev/tty ]]; then typeset -gi _z4h_tty_fd if sysopen -o cloexec -rwu _z4h_tty_fd -- /dev/tty 2>/dev/null; then typeset -gri _z4h_tty_fd else unset _z4h_tty_fd fi fi ``` And this is the `-z4h-cursor-show` function: ```sh function -z4h-cursor-show() { local cnorm=${${terminfo[cnorm]-}:/*$'\e[?25h'(|'\e'*)/$'\e[?25h'} [[ -t 1 ]] && print -rn -- $cnorm || print -rnu $_z4h_tty_fd -- $cnorm } ``` ### Steps to reproduce - Open Windows Terminal and run WSL (tested with Ubuntu and ArchWSL); - Install zsh4humans; - Edit `.zshrc`: - Delete the line `zstyle ':z4h:' start-tmux no` - Add these lines at the end of the file: ```sh zstyle ':z4h:' prompt-at-bottom yes z4h bindkey z4h-clear-screen-hard-bottom Ctrl+L ``` - Run `exec zsh` or `source ~/.zshrc` to apply the new changes; - Press <kbd>Ctrl-L</kbd>. ### Expected Behavior The `z4h-clear-screen-hard-bottom` command is meant to clear the scrollback and push the prompt to the bottom of the terminal. [Here](https://asciinema.org/a/372068) is an example of how it should work. ### Actual Behavior It seems that in Windows Terminal, for some reason, the prompt itself is also cleared: https://github.com/microsoft/terminal/assets/31555284/ed7348a3-d94c-4f7e-be57-065b5fb66be8 I opened an issue in the official zsh4humans repository, and the owner asked me to try to reproduce this issue in other terminal emulators. I tried it with Cmder, ConEmu and Hyper, and none of them share this issue. Does anyone know what could be causing this?
claunia added the Issue-BugArea-VTNeeds-ReproProduct-Terminal labels 2026-01-31 07:27:09 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Nov 29, 2023):

Can you try capturing the output of zsh with script/? That'll give us a raw blob of output we can use to debug and figure out what's going on here. Thanks!

@zadjii-msft commented on GitHub (Nov 29, 2023): Can you try capturing the output of zsh with [`script`](https://github.com/microsoft/terminal/wiki/Troubleshooting-Tips#script)/? That'll give us a raw blob of output we can use to debug and figure out what's going on here. Thanks!
Author
Owner

@araggohnxd commented on GitHub (Nov 30, 2023):

Hmm ok, something must have happened in the latest release that kinda fixed it. I just saw that my Terminal automatically updated to version 1.18.3181.0 and now the prompt isn't deleted, but the scrollback isn't properly cleared either (you can see there is still a scrollbar even after pressing Ctrl+L.

https://github.com/microsoft/terminal/assets/31555284/b2868675-7f9f-42fc-b34f-e407a6d7953b

That being said, here is the script generated file in the latest Terminal version. I basically just executed script and then pressed Ctrl+L:

typescript
Script started on 2023-11-29 21:25:21-03:00 [TERM="tmux-256color" TTY="/dev/pts/21" COLUMNS="229" LINES="61"]
[?1h=[?25l                                                                                                                                                                                                                                     



























































[?25h[?1000l[?1002l[?1003l[?1006l[?1005l[?25h[?1006l[?1000l[?1002l[?1003l[?2004l[>c[>q]0;machine[?25l



























































[?25h[?25h[?1006l[?1000l[?1002l[?1003l[?2004l[?25l



























































[?25h[?7727h[?7727h]133;A╭─    ~ ········································································································································································································   21:25:21  
╰─ ]133;B]133;A╭─    ~ ········································································································································································································   21:25:21  ╰─ ]133;B[?2004h]1337;RemoteHost=araggohnxd@machine]1337;CurrentDir=/home/araggohnxd]1337;ShellIntegrationVersion=12;shell=zsh]1337;RemoteHost=araggohnxd@machine]1337;CurrentDir=/home/araggohnxd]0;~[?25l[?25h]7;file://machine/home/araggohnxd\[?25l[?25h]9;9;//wsl.localhost/Arch/home/araggohnxd\]133;C;]133;D;0]1337;RemoteHost=araggohnxd@machine]1337;CurrentDir=/home/araggohnxd[?25l[?25h[?25l╭─    ~ ········································································································································································································ ✔  21:25:21 
╰─
M
M]133;A╭─    ~ ·············································································································································
@araggohnxd commented on GitHub (Nov 30, 2023): Hmm ok, something must have happened in the latest release that kinda fixed it. I just saw that my Terminal automatically updated to version 1.18.3181.0 and now the prompt isn't deleted, but the scrollback isn't properly cleared either (you can see there is still a scrollbar even after pressing <kbd>Ctrl+L</kbd>. https://github.com/microsoft/terminal/assets/31555284/b2868675-7f9f-42fc-b34f-e407a6d7953b That being said, here is the `script` generated file in the latest Terminal version. I basically just executed `script` and then pressed <kbd>Ctrl+L</kbd>: <details> <summary>typescript</summary> ```ts Script started on 2023-11-29 21:25:21-03:00 [TERM="tmux-256color" TTY="/dev/pts/21" COLUMNS="229" LINES="61"] [?1h=[?25l  [?25h[?1000l[?1002l[?1003l[?1006l[?1005l[?25h[?1006l[?1000l[?1002l[?1003l[?2004l[>c[>q]0;machine[?25l                                                            [?25h[?25h[?1006l[?1000l[?1002l[?1003l[?2004l[?25l                                                            [?25h[?7727h[?7727h]133;A╭─    ~ ········································································································································································································ ✔  21:25:21   ╰─ ]133;B]133;A╭─    ~ ········································································································································································································ ✔  21:25:21  ╰─ ]133;B[?2004h]1337;RemoteHost=araggohnxd@machine]1337;CurrentDir=/home/araggohnxd]1337;ShellIntegrationVersion=12;shell=zsh]1337;RemoteHost=araggohnxd@machine]1337;CurrentDir=/home/araggohnxd]0;~[?25l[?25h]7;file://machine/home/araggohnxd\[?25l[?25h]9;9;//wsl.localhost/Arch/home/araggohnxd\]133;C;]133;D;0]1337;RemoteHost=araggohnxd@machine]1337;CurrentDir=/home/araggohnxd[?25l[?25h[?25l╭─    ~ ········································································································································································································ ✔  21:25:21  ╰─ M M]133;A╭─    ~ ············································································································································· ``` </details>
Author
Owner

@araggohnxd commented on GitHub (Nov 30, 2023):

Today I booted up my PC again and it went back to deleting prompt. Yeah, I don't get it either.

EDIT: It seems that running script makes it stop deleting prompt and start to not fully clear the scrollback.

@araggohnxd commented on GitHub (Nov 30, 2023): Today I booted up my PC again and it went back to deleting prompt. Yeah, I don't get it either. EDIT: It seems that running `script` makes it stop deleting prompt and start to not fully clear the scrollback.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20897