Reputation: 176
I was looking for some alternative to system("cls")
that works on MacOS and I found this:
printf("\e[1;1H\e[2J");
However I do not know what this is doing.
Upvotes: 3
Views: 7235
Reputation: 829
if your language does't support \e
you can instead replace it with \x1b
or \033
.
These represent the same byte number, 27, written either in base 16 (hexadecimal) or base 8 (octal) respectively.
As hexidecimal:
printf("\x1b[1;1H\x1b[2J");
As octal:
printf("\033[1;1H\033[2J");
Upvotes: 0
Reputation: 150553
\e
is the escape character:
Terminals handle this character differently from normal text. It treats it as an ANSI escape code. For example, this character, followed by the [
character (U+005B), represents a control sequence introducer.
When a control sequence introducer is followed by a character representing a number, the by ;
, then by another number, then by H
, as in your example, the terminal will move the cursor to the position marked by the two numbers.
Upvotes: 1
Reputation: 328
\e
is escape and what that printf() line is telling the terminal to move the cursor to line 1 column 1 (\e[1;1H
) and to move all the text currently in the terminal to the scrollback buffer (\e[2J
).
These are ANSI escape codes and here's some resources:
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
https://bluesock.org/~willkg/dev/ansi.html
https://en.wikipedia.org/wiki/ANSI_escape_code (suggested by tadman)
Edit: I also recommend the use of \e[H\e[2J\e[3J
as that is what cls/clear prints. This tells the terminal to move the cursor to the top left corner (\e[H
), clear the screen (\e[2J
), and clear the scrollback buffer (\e[3J
).
Upvotes: 4