clinuxconsoleerase

Erase the current printed console line


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.


Solution

  • 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("\33[2K\r");