How does vi restore terminal content after quitting it?

后端 未结 2 2069
名媛妹妹
名媛妹妹 2020-12-01 18:55

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 o

2条回答
  •  忘掉有多难
    2020-12-01 19:48

    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 
    
    int main()
    {
        // clear the terminal
        printf("\033[2J\033[1;1H");
        printf("hello");
    
    }
    

    Example of switchting to alternate Buffer and back (xterm).

    #include 
    #include 
    
    int main()
    {
        printf("\033[?1049h\033[H");
        printf("hello\n");
        sleep(1);
        printf("bye");
        sleep(1);
        printf("\033[?1049l");
    }
    

提交回复
热议问题