Erase the current printed console line

Peter picture Peter · Oct 2, 2009 · Viewed 158.5k times · Source

How can I erase the current printed console line in C? I am working on a Linux system. For example -

printf("hello");
printf("bye");

I want to print bye on the same line in place of hello.

Answer

mouviciel picture mouviciel · Oct 2, 2009

You can use VT100 escape codes. Most terminals, including xterm, are VT100 aware. For erasing a line, this is ^[[2K. In C this gives:

printf("%c[2K", 27);