ANSI device drive

Source: Internet
Author: User

This is mentioned in "ANSI device driver" in book C/C ++ programmer practical Daquan-Excellent edition: C/C ++ best programming guide ".

It is similar in shell programming.

If you use a colored prompt to increase personalization, you need to use escape sequences. The escape sequence is a control command that allows shell to execute a special step. Generally, escape sequences start with ESC (which is also the reason for its naming ). In shell, it is represented as ^ [. This representation requires a little time to adapt, or you can use \ 033 to complete the same work (the esc ascii code is expressed in decimal format as 27, = 33 in octal format ). To directly input the escape sequence in shell, press Ctrl-V: Ctrl-v esc.

Escape Sequence

Function

Example

ESC [X; YH

Place the cursor in column Y of row x

ESC [10; 25 h

ESC [xa

Move the cursor up X rows

ESC [1A

ESC [XB

Move the cursor down x rows

ESC [2B

ESC [YC

Right Shift cursor column Y

ESC [10C

ESC [YD

Move cursor left column Y

ESC [10D

ESC [s

Store the current cursor position

ESC [s

ESC [u

Restore cursor position

ESC [u

ESC [2j

Clear the screen and move the cursor to the starting position

ESC [2j

ESC [K

Clear to end of line

ESC [K

 

C language example

Print "Hello word" in Green ":

# Include <stdio. h>

Int main (void ){

Const char * const Green = "\ 033 [0; 40; 32 m ";

Const char * const normal = "\ 033 [0 m ";

Printf ("% Shello world % s \ n", green, normal );

Return 0;

}

 

Another useful escape sequence is printf ("\ 033 [2j"), which has the same functions as system (clear. However, the header file unistd. H is not required.

You can use printf ("\ 033 [1 K") to delete a row.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.