Prompt issue after invoking "clearBuffer" #15667

Closed
opened 2026-01-31 04:45:01 +00:00 by claunia · 9 comments
Owner

Originally created by @elsaco on GitHub (Oct 23, 2021).

Windows Terminal version (or Windows build number)

Windows Terminal Preview ver. 1.12.2931.0

Other Software

Windows 10 ver. 10.0.19043.0
git ver. 2.33.0.windows.2

Steps to reproduce

  1. add an unbound key cord to settings.json and enable clearBuffer (crtl+shift+k in this case)
        {
           "command":
           {
             "action": "clearBuffer",
             "clear": "all"
           },
           "keys": "ctrl+shift+k"
          
        }
  1. fill buffer then invoke clearBuffer

Expected Behavior

  1. buffer cleared (it works!)

Actual Behavior

  1. after clearing the buffer the prompt is at the top of tab, however next input is shown at the bottom of tab.
  2. not git related as it happens with other commands

Example:

https://user-images.githubusercontent.com/3933920/138570174-62dbf11b-3d1f-4a66-927e-d0a7f2e7c3cf.mp4

Originally created by @elsaco on GitHub (Oct 23, 2021). ### Windows Terminal version (or Windows build number) Windows Terminal Preview ver. 1.12.2931.0 ### Other Software Windows 10 ver. 10.0.19043.0 git ver. 2.33.0.windows.2 ### Steps to reproduce 1. add an unbound key cord to settings.json and enable clearBuffer (crtl+shift+k in this case) ``` { "command": { "action": "clearBuffer", "clear": "all" }, "keys": "ctrl+shift+k" } ``` 2. fill buffer then invoke clearBuffer ### Expected Behavior 1. buffer cleared (it works!) ### Actual Behavior 1. after clearing the buffer the prompt is at the top of tab, however next input is shown at the bottom of tab. 2. not git related as it happens with other commands Example: https://user-images.githubusercontent.com/3933920/138570174-62dbf11b-3d1f-4a66-927e-d0a7f2e7c3cf.mp4
claunia added the Resolution-ExternalNeeds-Tag-Fix labels 2026-01-31 04:45:01 +00:00
Author
Owner

@serd2011 commented on GitHub (Oct 23, 2021):

I was able to reproduce it only in PowerShell and PowerShell Core.
cmd, wsl (Ubuntu and Debian) works fine.
I tested it at 670ae2bd1c

It is not necessary to fill the buffer completely.
You can use command palette ui instead of adding key binding.

@serd2011 commented on GitHub (Oct 23, 2021): I was able to reproduce it only in PowerShell and PowerShell Core. cmd, wsl (Ubuntu and Debian) works fine. I tested it at https://github.com/microsoft/terminal/commit/670ae2bd1c78fd2cc5b8e88f426eacab71fe1bfb It is not necessary to fill the buffer completely. You can use command palette ui instead of adding key binding.
Author
Owner

@yhmtsai commented on GitHub (Oct 23, 2021):

I also face this issue.
Terminal Preview version: 1.12.2931
It only happens in powershell. cmd/ubuntu in wsl work well
It still has space on the buffer after clearbuffer

Example:
clearbuffer_ps

@yhmtsai commented on GitHub (Oct 23, 2021): I also face this issue. Terminal Preview version: 1.12.2931 It only happens in powershell. cmd/ubuntu in wsl work well It still has space on the buffer after clearbuffer Example: ![clearbuffer_ps](https://user-images.githubusercontent.com/19565938/138573118-5cf2c038-8c3c-414b-bce7-022a20f95abf.gif)
Author
Owner

@pcbeard commented on GitHub (Nov 23, 2021):

Just reproduced this issue myself using the preview build. Clearly the cursor position isn't being updated.

@pcbeard commented on GitHub (Nov 23, 2021): Just reproduced this issue myself using the [preview build](https://www.microsoft.com/store/apps/9n8g5rfz9xk3?cid=storebadge&ocid=badge). Clearly the cursor position isn't being updated.
Author
Owner

@DHowett commented on GitHub (Nov 23, 2021):

PowerShell caches the cursor position every time it displays the prompt, and then after we reset the screen it moves the cursor back. This is effectively a /duplicate of https://github.com/PowerShell/PSReadLine/issues/964

@DHowett commented on GitHub (Nov 23, 2021): PowerShell caches the cursor position every time it displays the prompt, and then after we reset the screen it _moves the cursor back_. This is effectively a /duplicate of https://github.com/PowerShell/PSReadLine/issues/964
Author
Owner

@ghost commented on GitHub (Nov 23, 2021):

Hi! We've identified this issue as a duplicate of one that exists on somebody else's Issue Tracker. Please make sure you subscribe to the referenced external issue for future updates. Thanks for your report!

@ghost commented on GitHub (Nov 23, 2021): Hi! We've identified this issue as a duplicate of one that exists on somebody else's Issue Tracker. Please make sure you subscribe to the referenced external issue for future updates. Thanks for your report!
Author
Owner

@pcbeard commented on GitHub (Nov 23, 2021):

Interestingly enough, if you use the "cls" command in PowerShell or cmd, the right thing happens.

@pcbeard commented on GitHub (Nov 23, 2021): Interestingly enough, if you use the "cls" command in PowerShell or cmd, the right thing happens.
Author
Owner

@DHowett commented on GitHub (Nov 23, 2021):

@pcbeard indeed. Since cls (or Clear-Host) is implemented by PowerShell¹, it executes between prompts. Once it returns, it caches the new correct position. Under the hood, Terminal is pretty much doing exactly the same thing: emitting a sequence that requests that the screen be cleared².

Unfortunately, applications aren't designed to receive this sequence, only to send it. PowerShell doesn't actually handle \e[2J as input -- if you send it, it's not going to do anything with it. This is true of other shells, as well--bash, cmd (especially), fish, ...--as they do not expect to receive the "Erase in Display" control sequence as input. It's officially "not their job" to handle it, and so handle it they don't.

It turns out that there's no general way to tell an application that its screen has been cleared and that it needs to recalculate any positions it might have stored³. ☹️

¹ This issue doesn't seem to exist in cmd; it doesn't track the cursor position!
² To a rough first approximation.
³ @zadjii-msft . . . what if we pretend the window size changed when we Clear Buffer? How crazy would that be? It might also fix vim (!) though admittedly, I expect that one of the eight layers between us and vim would filter out a SIGWINCH where the sizes haven't actually changed...

@DHowett commented on GitHub (Nov 23, 2021): @pcbeard indeed. Since `cls` (or `Clear-Host`) is implemented by PowerShell¹, it executes between prompts. Once it returns, it caches the new correct position. Under the hood, Terminal is pretty much doing exactly the same thing: emitting a sequence that requests that the screen be cleared². Unfortunately, applications aren't designed to _receive_ this sequence, only to send it. PowerShell doesn't actually handle `\e[2J` as input -- if you send it, it's not going to do anything with it. This is true of other shells, as well--bash, cmd (especially), fish, ...--as they do not expect to receive the "Erase in Display" control sequence as input. It's officially "not their job" to handle it, and so handle it they don't. It turns out that there's no general way to tell an application that its screen has been cleared and that it needs to recalculate any positions it might have stored³. ☹️ ¹ This issue doesn't seem to exist in cmd; it doesn't track the cursor position! ² To a rough first approximation. ³ @zadjii-msft . . . what if we pretend the window size changed when we Clear Buffer? How crazy would that be? It might also fix vim (!) though admittedly, I expect that one of the eight layers between us and vim would filter out a `SIGWINCH` where the sizes _haven't actually changed_...
Author
Owner

@zadjii-msft commented on GitHub (Nov 23, 2021):

what if we pretend the window size changed when we Clear Buffer? How crazy would that be? It might also fix vim (!) though admittedly, I expect that one of the eight layers between us and vim would filter out a SIGWINCH where the sizes haven't actually changed...

as someone who's written a lot of our code that filters out size changes when the sizes haven't actually changed, I'd expect that it would get filtered

Actually who am I kidding. Considering the number of times I have had to write that code, I'd reckon there's a good opening here that might just work

@zadjii-msft commented on GitHub (Nov 23, 2021): > what if we pretend the window size changed when we Clear Buffer? How crazy would that be? It might also fix vim (!) though admittedly, I expect that one of the eight layers between us and vim would filter out a `SIGWINCH` where the sizes _haven't actually changed_... as someone who's written a _lot_ of our code that filters out size changes when the sizes haven't actually changed, ~I'd expect that it would get filtered~ Actually who am I kidding. Considering the number of times I have had to write that code, I'd reckon there's a good opening here that might just work
Author
Owner

@SirUppyPancakes commented on GitHub (Apr 12, 2022):

Not really familiar with the internals of how shells or terminals work, but an interesting workaround for this that I've been using is to set up my usual AuthHotKey script that I use for hotkeys to map ctrl+k to a sequence of ctrl+l (lowercase L), ctrl+k, which seems to prevent the prompt from sticking:

; Workaround for Powershell to fix cursor position when clearing buffer
#IfWinActive ahk_exe WindowsTerminal.exe
$^k::
    Send ^l
    Send ^k
Return
#IfWinActive
@SirUppyPancakes commented on GitHub (Apr 12, 2022): Not really familiar with the internals of how shells or terminals work, but an interesting workaround for this that I've been using is to set up my usual AuthHotKey script that I use for hotkeys to map `ctrl+k` to a sequence of `ctrl+l` (lowercase L), `ctrl+k`, which seems to prevent the prompt from sticking: ```ahk ; Workaround for Powershell to fix cursor position when clearing buffer #IfWinActive ahk_exe WindowsTerminal.exe $^k:: Send ^l Send ^k Return #IfWinActive ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15667