How to clear console without using spaces when "Wrap text output on resize" is enabled #5782

Closed
opened 2026-01-31 00:21:28 +00:00 by claunia · 2 comments
Owner

Originally created by @joaobzrr on GitHub (Jan 5, 2020).

I'm making a pager like less and I'm trying to figure out a way to clear the console that works and is fast. Clearing the console by overwriting everything with spaces is not working because the console understands spaces as actual characters instead of the absence of characters which is what I want, otherwise subsequent lines are shifted to the right because the spaces used for clearing anything after the end of the previous line get wrapped when the console resizes. So, is there any other way to do this? I also need it to be reasonably fast: I've tried FillConsoleOutputCharacter(), and ScrollConsoleScreenBuffer() but both resulted in flickering.

Originally created by @joaobzrr on GitHub (Jan 5, 2020). I'm making a pager like [_less_](https://en.wikipedia.org/wiki/Less_(Unix)) and I'm trying to figure out a way to clear the console that works and is fast. Clearing the console by overwriting everything with spaces is not working because the console understands spaces as actual characters instead of the absence of characters which is what I want, otherwise subsequent lines are shifted to the right because the spaces used for clearing anything after the end of the previous line get wrapped when the console resizes. So, is there any other way to do this? I also need it to be reasonably fast: I've tried FillConsoleOutputCharacter(), and ScrollConsoleScreenBuffer() but both resulted in flickering.
Author
Owner

@zadjii-msft commented on GitHub (Jan 6, 2020):

I'd recommend using VT sequences to accomplish what you want. These are special strings of characters that instruct a terminal to perform an action like an API call, but they're available cross-platform, unlike the Console API.

You'll need to make you application manually enable VT mode, by calling SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING.

The sequence that is probably the most relevant to you is EL - Erase in Line. If you write the following string to the console, it'll clear the rest of the line to the right of the cursor: "\x1b[K"

  • \x1b is the ESC character
  • \x1b[ together is the CSI, the "Control Sequence Introducer"
@zadjii-msft commented on GitHub (Jan 6, 2020): I'd recommend using [VT sequences](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html) to accomplish what you want. These are special strings of characters that instruct a terminal to perform an action _like_ an API call, but they're available cross-platform, unlike the Console API. You'll need to make you application manually enable VT mode, by calling [`SetConsoleMode`](https://docs.microsoft.com/en-us/windows/console/setconsolemode) with `ENABLE_VIRTUAL_TERMINAL_PROCESSING`. The sequence that is probably the most relevant to you is EL - Erase in Line. If you write the following string to the console, it'll clear the rest of the line to the right of the cursor: ```"\x1b[K"``` * `\x1b` is the `ESC` character * `\x1b[` together is the CSI, the "Control Sequence Introducer"
Author
Owner

@DHowett-MSFT commented on GitHub (Jan 6, 2020):

Yeah, it's best to switch to VT instead of printing a buuuuuunch of spaces. Closing this one as answered. Thanks!

@DHowett-MSFT commented on GitHub (Jan 6, 2020): Yeah, it's best to switch to VT instead of printing a buuuuuunch of spaces. Closing this one as answered. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#5782