Save and Restore Terminal Content

Save and restore terminal content

You should use the alternate screen terminal capability. See
Using the "alternate screen" in a bash script

An answer to "how to use the alternate screen":

This example should illustrate:

#!/bin/sh
: <<desc
Shows the top of /etc/passwd on the terminal for 1 second
and then restores the terminal to exactly how it was
desc

tput smcup #save previous state

head -n$(tput lines) /etc/passwd #get a screenful of lines
sleep 1

tput rmcup #restore previous state

This'll only work on a terminal has the smcup and rmcup capabilities (e.g., not on Linux console (=a virtual console)).
Terminal capabilities can be inspected with infocmp.

On a terminal that doesn't support it, my tput smcup simply return an exit status of 1 without outputting the escape sequence.


Note:

If you intend to redirect the output, you might want to write the escape sequences directly to /dev/tty so as to not dirty your stdout with them:

exec 3>&1 #save old stdout
exec 1>/dev/tty #write directly to terminal by default
#...
cat /etc/passwd >&3 #write actual intended output to the original stdout
#...

How to save and restore terminal sessions for a specific IntelliJ project?

Update: this is implemented now in 2018.3 EAP.

Original answer

It's not possible at the moment, please vote for the related requests:

  • IDEA-117946 Save terminal tabs between sessions
  • IDEA-134884 Allow scripted opening, renaming & content of terminal windows

How does vi restore terminal content after quitting it?

Vi flips to the alternate screen buffer, supported by terminals. This is achieved using escape sequences. See this link for full details.

The termcap entry for these are 'ti' to enter, and 'te' to exit full-screen mode.

As @Celada points out below, hardcoding xterm escape sequences is not a Good Idea™, because the sequences vary according to $TERM, for example:


xterm-color
ti: <Esc> 7 <Esc> [ ? 47 h
te: <Esc> [ 2 J <Esc> [ ? 4 7 l <Esc> 8

xterm-256color
ti: <Esc> [ ? 1 0 4 9 h
te: <Esc> [ ? 1 0 4 9 l

On the other hand, xterm support is very broad these days among non-xterm terminals. Supporting only xterm is unlikely to cause problems, except for users with exotic or obsolete $TERM settings. Source: I support products that do this.



Related Topics



Leave a reply



Submit