Whilst I know that tput rmcup
returns from the "alternate screen" (referred to as "cup mode" in man 5 terminfo
) and restores the saved screen, it does have the side-effect of repositioning the cursor.
So if tput smcup
was called, tput rmcup
restores the screen and repositions the cursor, but if you then type a few more commands or press enter a few times and then use tput rmcup
again, the cursor returns to the original saved position.
One use case is in a bash
script that replays a terminal recording [using scriptreplay
]: if the script ends prematurely without [the equivalent of] calling tput rmcup
then I'd like to be able to detect that in my bash
script and call tput rmcup
automatically.
So in short, I'd like to be able to determine what the current screen state is; i.e., is it the "alternate screen" or is it the "normal screen"?
If you're using xterm, then you can ask it what mode it's using. Although the alternate-screen feature is an xterm feature rather than "DEC", the setting is grouped with the
DECSET
modes described in XTerm Control Sequences:The
DECRQM
control can be used to query the terminal:That is, your script could
and read the result back, expecting something like
\033[?1049;1$y
Here is a quick demo:
Of course, if you're not using xterm, your mileage may vary...