0

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

Jongware
  • 22,200
  • 8
  • 54
  • 100
Zorgatone
  • 4,176
  • 4
  • 30
  • 47
  • 1
    are you mean like `reset` command? – Jayesh Bhoi Jul 15 '14 at 09:30
  • @Jayesh isn't reset a bash command that will reload the bash shell? I don't know if that'll do the trick, but system('reset') would be a bad choice imho – Zorgatone Jul 15 '14 at 09:32
  • Just tested it, it will cause a bad delay beetween everytime it's called, displaying a white screen.. That's not what I want but if I can fix the delay that's pretty much what I wanted to do – Zorgatone Jul 15 '14 at 09:36
  • 2
    I think `printf '\033c'` will clear all. including scroll bar also. – Jayesh Bhoi Jul 15 '14 at 09:36
  • is this what you are saying? [code] #include #include 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; } [/code] – rm_beginners Jul 15 '14 at 09:36
  • @Jayesh how did you learn how those ANSI escape characters work? – Zorgatone Jul 15 '14 at 10:23
  • @Zorgatone see http://www.tldp.org/HOWTO/Keyboard-and-Console-HOWTO-4.html – Jayesh Bhoi Jul 15 '14 at 10:30
  • @Jayesh Thanks man! However see my updated thread. I'm having issues now with cursor visibility. You're code is nice but it will show the cursor again if it was hidden before.. Do you know a solution to this? Cheers – Zorgatone Jul 15 '14 at 10:34
  • See also: https://stackoverflow.com/questions/5367068/clear-a-terminal-screen-for-real – AAAfarmclub Oct 06 '18 at 08:38

5 Answers5

5

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

Zorgatone
  • 4,176
  • 4
  • 30
  • 47
2
#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); :)

rm_beginners
  • 164
  • 5
  • @Jayesh nope.. Btw I said not to use conio.h in my question, because that's only windows/DOS. I see he didn't read it my whole question – Zorgatone Jul 15 '14 at 09:43
  • if not this thread may help http://stackoverflow.com/questions/11823904/subsititue-of-getchar-and-clrscr-in-c-for-linux – rm_beginners Jul 15 '14 at 09:44
  • 1
    @rm_beginners but OP says without curses/ncurses. – Jayesh Bhoi Jul 15 '14 at 09:46
  • @rm_beginners I solved using the Jayesh solution: printf("\033c"); and no, I don't want to use curses/ncurses – Zorgatone Jul 15 '14 at 09:47
  • @rm_beginners Please take some time to read again my question. system("clear") will add new empty lines and scroll down the screen, I do not want that! I want something similar to system("reset") but this is not working well because it's delayed. printf("\033c") worked for now – Zorgatone Jul 15 '14 at 09:58
2

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.

computerquip
  • 123
  • 7
0

I am using this one:

write(STDOUT_FILENO, "\x1b[2J", 4);
exebook
  • 32,014
  • 33
  • 141
  • 226
0

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

Clears the screen and the scrollback buffer.