How does a program like vi or man or any other program replace the terminal content with the program's own contents then after quitting those programs they bring back the old terminal content?
-
by sending control sequences to the terminal (xterm, vt-220) or ncurses (like mc), see this question: http://stackoverflow.com/questions/8476332/writing-a-real-interactive-terminal-program-like-vim-htop-in-c-c-witho – pce Feb 23 '13 at 19:00
-
1I read that question but I still can't figure the control sequence I can send to the terminal. I am using Solaris 11.0. – ASelim Feb 23 '13 at 19:41
2 Answers
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.

- 14,495
- 5
- 33
- 67
-
-
2@PaulBeckingham Do you know the [terminfo](http://en.wikipedia.org/wiki/Terminfo) capability name for doing this (I don't). If you do, I suggest adding it to your answer because that's what software should use, rather than a hardcoded escape sequence for a particular type of terminal, in order to be compatible with any type of terminal (as set by `$TERM`). – Celada Feb 23 '13 at 20:11
-
1@PaulBeckingham Oh, actually there are right there in the link your posted: **smcup** and **rmcup**. – Celada Feb 23 '13 at 20:14
-
I still don't understand how can I put all of this in a C program? I can only use the ?1049h escape character but I don't know how to use ti or te. – ASelim Feb 23 '13 at 20:51
-
Try "man termcap". You'll see an API for getting the "ti" and "te" values. Then you just emit them on stdout. @pce shows you how to *hard-code* this. You should use termcap and printf. – Paul Beckingham Feb 23 '13 at 20:54
By sending control sequences to the terminal (xterm, vt-220) or using ncurses (like mc).
A ANSI Escape Sequence starts with ESC (\033 octal) [. ; separates Numbers.
C Example that clears the Screen and moves the cursor to 1,1.
#include <stdio.h>
int main()
{
// clear the terminal
printf("\033[2J\033[1;1H");
printf("hello");
}
Example of switchting to alternate Buffer and back (xterm).
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("\033[?1049h\033[H");
printf("hello\n");
sleep(1);
printf("bye");
sleep(1);
printf("\033[?1049l");
}

- 5,571
- 2
- 20
- 25
-
I don't know but it works. It shows an alternate screen with output then switches back to normal screen after finishing execution. – ASelim Feb 23 '13 at 20:01
-
Why 1049? Because xterm supports hundreds of different escape sequences, for color, scrolling rectangles, clearing screen, cursor motion, etc etc. – Paul Beckingham Feb 23 '13 at 20:32
-
-
1@PaulBeckingham thanks, i replaced /Control/ with /Ansi Escape/ Sequence ( http://ascii-table.com/ansi-escape-sequences.php ) – pce Feb 23 '13 at 20:49
-
1
-
1Regarding 1049 - originally there was 47, which had issues, and I added 1047/1048 (to make them distinct), and finally 1049. It is in the xterm FAQ of course: http://invisible-island.net/xterm/xterm.faq.html#xterm_tite – Thomas Dickey Mar 22 '15 at 00:12