Zorgatone
Zorgatone

Reputation: 4283

How can you clear (reset) the screen in unix/posix? (not curses/newlines)

I'd like to know (if it's possible) how can you clear/reset the terminal screen in linux/mac/unix (not DOS) like you would do on windows/DOS with clrscr() from "conio.h". I know there are similar questions here and on the web in general, but I wasn't able to find one that answered my particular case.

ATTENTION: I know about curses/ncurses and solutions that emulate system("clear") but that's not what I want. I want to reset completely the terminal buffer (i.e. I don't want to scroll down or add newlines to clear the screen), without using curses/ncurses please (I don't like the ncurses screen mode, I want to stick with the default mode).

Is it possible or I'm asking something impossible? :P I'm trying to make a console-game (not exactly a roguelike) without curses, and I don't like to see what I printed on the screen before clearing it just by scrolling up.

EDIT: I've tried system("reset"), that's not a nice way using a system call, and it's getting a bad delay using that command but it's close to what I want to do.. Is there some kind of function/library that can do something similar?

I think that'll be a good solution as well to do something like move(0,0) and then print again what I need or just blank space (this way I won't have scrolling and the old text above it). But I don't know if there's a library that will do that without going un curses mode.

For now see my own answer below, I'm using

printf("\033c");

this is working fine for now and solved my problem. If anybody knows any issue with this solution please let me know. I have an issue with the cursor visibility. If it was hidden this code will show it again, do you know a fix for this?

Thanks,

Zorgatone

Upvotes: 0

Views: 4037

Answers (5)

user11545776
user11545776

Reputation: 65

alias cls='printf "\e[3J\033c"'

Clears the screen and the scrollback buffer.

Upvotes: 0

exebook
exebook

Reputation: 33900

I am using this one:

write(STDOUT_FILENO, "\x1b[2J", 4);

Upvotes: 0

Zorgatone
Zorgatone

Reputation: 4283

I've just discovered thanks to Jayesh that this escape code will clear my screen correctly, thanks!

printf("\033c");

EDIT: need fix! This will set the cursor visible if it was hidden previously.. How do I get the same thing without changing the cursor visibility?

If anybody will point out any issue about portability on posix/unix (linux/mac) systems, I will update the answer with a better solution.

Cheers

Upvotes: 5

computerquip
computerquip

Reputation: 133

For POSIX, you can use terminfo. Request the "clear" command using tigetstr and output the command using putp.

For Windows, you would be looking at using FillConsoleOutputCharacter and filling the console with spaces.

Then just abstract the two methods and you have a cross-platform mechanism.

Upvotes: 2

rm_beginners
rm_beginners

Reputation: 159

#include<stdio.h>
#include<conio.h>

main()
{
   printf("Press any key to run clrscr().\n");
   getch();
   clrscr();
   printf("After clearing the screen.\n");
   printf("Press any key to exit..\n");
   getch();
   return 0;
}

in linux system(clear); :)

Upvotes: 2

Related Questions