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.