41

I'm using chez-scheme and I can't find a way to clear the screen completely. (If someone knows a better way than printing I'd be interested in that too but it's not my question here)

From what I can find clearing the screen by ^L (control-L) or giving the clear command (in bash at least) is equivalent to outputting ASCII character 12: Form feed. However, printing this does nothing. If I use (display (integer->char 12)) it just prints a newline. Another way to encode this character is \f (analogous to \n for newline), but in Python print("\f") as well as in Scheme (display "\f") is just a newline.

Is my understanding of the meaning of ASCII 12 just wrong, or are implementations lacking?

Is there any way to clear the screen that should work across languages, analogous to \n for a newline?

2
  • 1
    Your basic understanding is correct but the available functionality depends on the precise output system calls used by the Scheme implementation as well as the terminal environment etc.
    – tripleee
    Commented Jun 12, 2016 at 14:18
  • The "terminal environment" caveat is critical. The environment variable TERM tells applications which terminal is in use; to be well behaved and portable, they should look up the operation they want to perform in the termcap/terminfo database for that terminal type, rather than hardcoding an escape sequence. Commented Jun 12, 2016 at 16:58

3 Answers 3

97

If you want to clear the screen, the "ANSI" sequence in a printf

\033[2J

clears the entire screen, e.g.,

printf '\033[2J'

The command-line clear program uses this, along with moving the cursor to the "home" position, again an "ANSI" sequence:

\033[H

The program gets the information from the terminal database. For example, for TERM=vt100, it might see this (using \E as \033):

clear=\E[H\E[J$<50>

(the $<50> indicates padding needed for real VT100s). You might notice that the 2 is absent from this string. That is because the cursor is first moved to the home (upper left) position, and the 2 (entire screen) is not necessary. Eliminating that from the string made VT100s a little faster.

On the other hand, if you just want to reset the terminal, you can use the VT100-style RIS:

\033c

but that has side-effects, besides not being in ECMA-48. These bug reports were for side-effects of \033c:

Further reading:

CSI Ps J  Erase in Display (ED).
            Ps = 0  -> Erase Below (default).
            Ps = 1  -> Erase Above.
            Ps = 2  -> Erase All.
            Ps = 3  -> Erase Saved Lines (xterm).
0
7

You can print \033c which resets the terminal:

petite -q <<< '(display "\033c")'

\033 is escape and c is literal c.

I can't give you any information about how widely this is supported.

5
  • 1
    What does 033 actually refer to?
    – Lara
    Commented Jun 12, 2016 at 14:37
  • 1
    @Darklightus \033 is an octal escape sequence for escape, see the ascii table for reference: man ascii Commented Jun 12, 2016 at 14:38
  • 2
    It's not different shells you need to test against, but different terminals. The shell has no role in interpreting terminal directives. Commented Jun 12, 2016 at 16:53
  • Interesting. I'm not familiar with qsh -- does it claim to be POSIX compliant? Were you using printf? Commented Jun 12, 2016 at 17:01
  • Based on wikipedia it is based on POSIX and X/Open standards
    – Sylwester
    Commented Jun 13, 2016 at 23:09
1

For C# and JavaScript (and many others), you have to use \x1b instead of \033 as follows:

Console.WriteLine("\x1b[2J\x1b[H"); // C#
console.log("\x1b[2J\x1b[H"); // Node.JS

Keep in mind that some poorly implemented pseudoterminals will break if you do this.

Not the answer you're looking for? Browse other questions tagged or ask your own question.