r/PowerShell • u/Affectionate-Bend721 • May 05 '26
Just print A^[[A^[[A^[[ when I try to scroll up
I want to scroll up in the terminal in VSS, and it always works, only not when it's inside a screen session. How to scroll up? Ctrl+a didn't work, ctrl PgUp didn't work.
0
Upvotes
8
u/420GB May 06 '26
Scrolling is a feature of the terminal, not the shell, so this is not related to PowerShell.
4
u/dodexahedron May 09 '26 edited May 09 '26
Screen, tmux, and others put you in the alternate screen buffer. Mouse scroll has a different meaning in that mode by default. Anything that takes over the screen and then restores it on exit behaves this way.
If you don't exit cleanly, you can be stuck in that mode even after exiting the remote session. It is not persistent, but it is still pretty annoying when it happens.
To fix it, you can write the ANSI sequence to switch back to the main buffer yourself, or you can cheat by launching an application that uses the alternate buffer (tmux, man, less, etc) and cleanly exiting that application, at which point it emits that sequence for you, with the same effect.
(The quotes are necessary)
(Also: The equivalent sequence must be written in whatever shell you are using, since it's what is going to send the string to the terminal. So if you're in bash inside ssh inside powershell, you need to use bash syntax.)
DO NOT emit that if you are currently inside screen or tmux. If you do, you will need to switch back to the alternate buffer to get it back.
An ssh session that got terminated due to connection loss can easily cause you to be stuck in the alternate buffer since it didn't emit the above sequence to swap back.
It works because there is only one main buffer and one alternate buffer.
If you are asking how to fix it while inside screen, tmux, etc, then you have to change the mode for the alternate buffer. But you need to do it in a way that both the local terminal emulator and the remote shell understand, so just writing the sequence to change the mouse mode isn't always a good idea. It may also only affect the remote PTY, rather than your local PTY, depending on what does or does not intercept the sequence along the way. Doing it in the outermost session is your best bet.
For a persistent fix, look into the configuration of the application itself to fix the behavior to how you want it. Tmux has a setting for this explicitly. I don't think screen does. But you're also not going to get true scrollback of the alternate screen buffer - just emulation of that, provided by tmux, as if you went into copy mode.
If you want to try it in screen by changing the mouse mode, emit
\033[?1007hto the console. Use l instead of h to turn it back off in the current terminal. But even if it works, understand that it likely will not persist across window swaps and almost definitely won't across suspending and resuming screen, snd definitely will not persist between connections, since the shell and other applications all emit those sequences naturally as part of their normal operation if the terminal has indicated support for it (all xterm derivatives do, including WT).Side note: "h" and "l" mean "high" and "low" (think bits in a control register, and then just used by convention thereafter) if those letters seemed arbitrary. Old VT100 terminals introduced the idea, so it was relevant then.
Another Side note: The no-scrollback behavior for the alternate buffer was exactly how VT100 terminals behaved, so that is still how it is done to this day. Terminal emulators that keep scrollback history even while in that mode are doing their own thing, and the results are weird and useless for most applications that use the alternate buffer.
YASN: If an application that switched to the alternate buffer did not clear it before swapping back to the main buffer, you can actually still return to the alternate buffer again after exit, to see what was there. There are only the two, and only the active buffer can be written to by anyone via normal means (e.g. you in theory could grab a pointer to that memory and write to it directly, but that's always true and always smartn't). Clearing that buffer does not save memory or anything. It is a fixed structure allocated at launch. But it can be an information disclosure side channel, so you should clear it before exit if your application uses it.
1
u/I_see_farts May 06 '26
Shift + PgUp?
1
u/Ferretau May 06 '26
Whats the screen session connected to? It sounds like the terminal type for the session is different to the terminal type VSS is using and as a result the "up" key translation isn't correct for the remote terminal.
1
u/purplemonkeymad May 06 '26
In screen best way to scroll is probably copy mode: Control+a, [. then you can use arrow keys/pageup/down etc. Press q to cancel out of copy mode IIRC.