3

I know this kind of questions are asked frequently, but I think this one is a little different and needed to be asked.

The new Windows console supports ANSI (VT100) control codes: ANSI/VT100 control codes & Windows document: the control codes.

However, ESC[2J doesn't really "clear" the screen, it just scrolls down to "hide" printed contents. Only ESC[H+ESC[J really "clears" the currently showed contents on the screen. Is this a bug or it is designed to do this? Is it written on some kind of documentation?

Please run this .bat to see what I mean:

@echo off
echo hello0
echo hello1
echo hello2
echo hello3
echo hello4
echo hello5
echo hello6
echo hello7
echo hello8
echo hello9
pause
echo [2J
pause
echo [H[J
pause

Or run this .py(Python 2):

import sys
from ctypes import windll
windll.kernel32.SetConsoleMode(windll.kernel32.GetStdHandle(-11), 7)
#set ansi(vt100) control code interpreted
#https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console
show = lambda s: sys.stdout.write(s)
for i in range(10):
    print("\x1b[30;47m hello \x1b[0m%d"%i)
raw_input()
show("\x1b[2J")
raw_input()
show("\x1b[H\x1b[J")
raw_input()

As you can see when running this simple script, ESC[2J just "scrolls down" to "make your screen clear", the contents are still there.

This is a little hard to explain, please comment if you don't understand what I mean, thank you!

Another small question: why cls command is slower than ESC[2J or ESC[H+ESC[J?

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Kirk
  • 446
  • 4
  • 18
  • GNOME Terminal's implementation of `CSI "2J"` is similar to the Windows console, except it only scrolls down by as much as necessary to clear the screen, while the Windows console scrolls by an entire window if possible. Also, CMD's `cls` command isn't the same as `CSI "H" CSI "J"` since "H" is relative to the current visible window, whereas `cls` clears the entire console screen buffer. – Eryk Sun Feb 06 '18 at 02:36
  • FYI, virtual terminal emulation has nothing to do with CMD. It merely enables this mode in its attached console, which your Python code also shows (that code is not very good or robust, but it's not the topic here). If you want VT mode enabled by default, set a DWORD value of 1 named "VirtualTerminalLevel" in "HKCU\Console". – Eryk Sun Feb 06 '18 at 02:44
  • So lots of terminals behave like that, `cls` clears all buffer, console probably equals to terminal and they control the IO, and CMD only interprets the commands. Learned a lot, thanks! – Kirk Feb 06 '18 at 13:04
  • I'm sure they're trying to emulate common Linux terminals since this feature was added to support the new Linux subsystem. It supports complex interfaces like text editors and tmux. – Eryk Sun Feb 06 '18 at 21:18
  • Yeah, Microsoft really wants Linux developers to use their system. – Kirk Feb 08 '18 at 15:32

2 Answers2

3

The issue withESC[2J was "known" in older versions of Windows which supported control sequences. That is, it should, but did not clear the whole screen. You might find some recent comment. No one's actually tested the recently reinstated Windows control sequences to point out compatibility with other terminals.

A console window has a "buffer" holding the visible characters plus the scrollback, which cls clears entirely, presumably taking more time than clearing just the visible characters.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • So it's "known but not documented"... Guess the only fast way to clear the screen is "`ESC[H`+`ESC[J`" – Kirk Feb 05 '18 at 12:23
  • I recalled it as something well-known when I added [ms-vt100](https://invisible-island.net/ncurses/terminfo.src.html#tic-ms-vt100), but my comments didn't elaborate, and finding details would take time... – Thomas Dickey Feb 05 '18 at 21:17
  • I was just curious about why console works in that way for `ESC[2J`, so there's no need to take your time to find more details, really thanks! – Kirk Feb 06 '18 at 13:18
0

Quite old thread, but printf("\x1bc"); use to work for me.

EduOak
  • 37
  • 3
  • "Reset terminal to initial state" (ESCc) isn't the same thing as clear screen – Moose Morals Jan 25 '23 at 18:44
  • I know it is not the same thing... what I meant is it works for me in the sense it actually clear the screen and remove its content. In my use case ESCc is enough and I suggested it because it could work for someone's else use case. – EduOak Jan 26 '23 at 19:32