C implementation of clear command

Posted: May 17, 2012 in Technology
Tags: , , ,

Following is the code to clear the screen on terminal.

main() {write(1, "\33[H\33[2J", 7);}

Above program does nothing but writes an escape sequence on screen. Escape sequences are used to control the display properties like graphics,control cursor movements and key reassignment. Above one is specific to clear the screen. \33[H sends the cursor to home location and \33[2J clears the text on screen. It can also be used interchangeably i.e first clear the screen and then send the cursor to home location since printing of terminal info (like root@localhost] ) takes place after executing the complete process though if you write the separate programs for both escape sequences individually and then execute them sequencially, output may differ based on order of execution.

To read about what are the escape sequences, refer to http://en.wikipedia.org/wiki/Escape_sequence

Here you can find a complete list of escape sequences. Have fun with your terminal 🙂

  1. Arun says:

    Reblogged this on Old Papyrus and commented:
    A good one from Kapil. Posted with minor modificaitons and a non-working URL fixed (I guess he will be notified of the reblog).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s