Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Console.Clear() doesn't clear the scrollback buffer on Unix-like platforms #34463
Indeed, not just clearing the current terminal window (screen), but also the scrollback buffer is the typical use case, and it is how this method has always worked on Windows.
By contrast, the Unix implementation currently only clears the screen, which makes for an inconsistent cross-platform experience.
While there is no POSIX-compliant way to clear the scrollback buffer (only clearing the screen is mandated by POSIX, via
In practice, the macOS terminal application and the one on Ubuntu, for instance, do support this escape sequence - generally, terminal emulators based on the X Window System.
I don't know if there are popular terminal emulators out there that do not support it, but even a best-effort implementation would be useful.
Here's a workaround that demonstrates use of the escape sequence:
// Clears the screen and the scrollback buffer in xterm-compatible terminals. Console.Clear(); Console.WriteLine("\x1b[3J")
 Whether there should be an opt-in for predictably clearing only the screen across platforms, while leaving the scrollback buffer intact, is a separate question.
referenced this issue
Jan 9, 2019
Not clearing it was not a goal. If there's a "safe" way to do it whenever it's possible (by safe I mean not emitting garbage to stdout when the particular escape code isn't supported by the terminal in use), such as relying on some reliable way to query that a compatible terminal is being used, I'm fine with doing so.
Based on the answers posted at https://unix.stackexchange.com/q/93376/54804, this should be as simple as:
# ... perform screen clearing (as before) # In compatible terminals, also clear the scrollback buffer. if (Environment.GetEnvironmentVariable("TERM").StartsWith("xterm")) Console.WriteLine("\x1b[3J");
xterm-compatible terminal emulators set env. var.
You should be able to query the terminfo db for the E3 extension, no?
See user_caps(5) under "Recognized capabilities":
Thanks, @khellang, but in practice neither
Consistent with that, the requisite escape sequence,
I know little about terminal-info databases, so maybe I'm missing something.
@danmosemsft: I'll give it a shot.
It sounds like the right thing to do is:
If performance is a concern, we could do (b) first, but I presume it won't matter.