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.
Post where I found this
\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).
\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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With