I want to know: how to clean screen on an UNIX-based system? I searched on the Internet, but I've just found how to do it on Windows: system("CLS") I don't want exactly to clean cpmpletely the screen, but I want to open a "new page", such as in the NANO and VI editors. Thanks
10 Answers
Maybe you can make use of escape codes
#include <stdio.h>
#define clear() printf("\033[H\033[J")
int main(void)
{
clear();
return 0;
}
But keep in mind that this method is not compatible with all terminals

- 39,972
- 7
- 52
- 94
-
1
-
8[ANSI/VT100 Terminal Control Escape Sequences](http://www.termsys.demon.co.uk/vtansi.htm) – David Ranieri Jun 24 '13 at 14:02
-
-
https://www.ecse.rpi.edu/courses/CStudio/Old%20MPS%20Labs/MPS_ANSI_Lab_Ex2.pdf This URL says that `\033[H` will position the cursor to the home position and `\033[J` stands for erase lines below. `\033[2J` erases the screen & moves cursor to the home position so you might only need this. – Silviu Burcea Sep 15 '21 at 14:47
You can use the following code which use termcap for clear screen. (don't forget to link with the library)
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
void clear_screen()
{
char buf[1024];
char *str;
tgetent(buf, getenv("TERM"));
str = tgetstr("cl", NULL);
fputs(str, stdout);
}

- 455
- 2
- 14
-
4+1, on Debian you have to install `libncurses5-dev` and compile using `-lncurses` – David Ranieri Jun 24 '13 at 17:14
Portable UNIX code should be using the terminfo database for all cursor and screen manipulation. This is what libraries like curses
uses to achieve its effects like windowing and so forth.
The terminfo database maintains a list of capabailities (like clear
which is what you would use to clear the screen and send the cursor to the top). It maintains such capabilities for a wide range of devices so that you don't have to worry about whether you're using a Linux console or a (very dated) VT52 terminal.
As to how you get the character streams for certain operations, you can choose the time-honored but rather horrible method of just using system
to do it:
system ("tput clear");
Or you can capture the output of that command to a buffer so later use involve only outputting the characters rather than re-running the command:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static char scrTxtCls[20]; static size_t scrSzCls;
// Do this once.
FILE *fp = popen ("tput clear", "r");
scrSzCls = fread (scrTxtCls, 1, sizeof(scrTxtCls), fp);
pclose (fp);
if (scrSzCls == sizeof(scrTxtCls)) {
actIntelligently ("you may want to increase buffer size");
}
// Do this whenever you want to clear the screen.
write (1, cls, clssz);
Or, you can link with ncurses
and use its API to get whatever capabilities you want, though this might drag in quite a bit of stuff for something as simple as clearing the screen. Still, it's an option to be considered seriously since it gives you a lot more flexibility.

- 854,327
- 234
- 1,573
- 1,953
It is usually not a matter of just clearing the screen, but of making a terminal aware application.
You should use the ncurses library and read the NCURSES programming HowTo
(You could perhaps use some ANSI escape codes as David RF answered, but I don't think it is a good idea)

- 1
- 1

- 223,805
- 18
- 296
- 547
You can achieve this using CSI sequences:
#include <stdio.h>
int main()
{
printf("\x1b[H\x1b[J");
}
What does \x1b[H
?
Actually it is the same as \x1b[1;1;H
, it means that it will move the cursor to row 1 and column 1.
What does \x1b[J
a.k.a \x1b[0;J
?
If n is 0 or missing, it will clear from cursor to end of screen.
Source: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences

- 388
- 1
- 4
- 14
-
1Wouldn't `\x1b[2J` be enough for moving cursor and clearing the screen? – Silviu Burcea Sep 15 '21 at 14:53
Just use #include<stdlib.h>
after #include<stdio.h>
.
Then you can use the command system("clear");
after main() {
i.e:
#include<stdio.h>
#include<stdlib.h>
int main()
{
system("clear");
After these commands you can continue with your program.
Hope this helps :)

- 2,108
- 9
- 18
- 29

- 21
- 1
To clear the screen using termcaps, use this :
write(1, tgetstr("cl", 0), strlen(tgetstr("cl", 0)));

- 822
- 9
- 18
Use system("clear");
with header #include <stdlib.h>
(for C Language) or #include <cstdlib>
(for C++).

- 11
- 3