Configure the prompt and color for the shell Terminal

Source: Internet
Author: User
ANSI compatible terminals (such as xterm, rxvt, and konsole) can display text in color, not just black and white. This article demonstrates how to use bold and colored text. In this article, we use Bashshell as an example. Because the escape sequences in different terminals are not exactly the same, I use ANSI terminals in this article. The shell color configuration appears on ANSI compatible terminals (such as xterm, rxvt, and konsole). you can use color to display text, not just black and white. This article demonstrates how to use bold and colored text.

In this article, I use Bash shell as an example. Because the escape sequences in different terminals are not exactly the same, I use ANSI terminals in this article.

Shell color configuration appears in the bash personal configuration file~ /. BashrcOr a global configuration file./Etc/bashrc. You can useBashrcTo set the appearance of the prompt.

In general, the default. bashrc should be:

~ /. Bashrc

Export PS1 = '\ u @ \ h: \ w \ $'

The above means: user @ host: current directory $

--> This is also a shell prompt that everyone is very familiar.

Escape sequence

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 ). ^ [. This representation requires a little time to adapt, or you can use \ 033 to complete the same work (esc ascii code is expressed in decimal format as 27, equal to 033 in octal format ).

To directly input the escape sequence in the shell, press ctrl-v:CTRL-v ESC.

Next we will go to the topic and explain how to use shell colors. I used my own shell color settings for detailed instructions.

 

Code:

PS1 = "\ [\ 033 [1; 32; 40 m [\ 033 [0; 32; 40m \ u @ \ h: \ 033 [1; 35; 40m \ w \ 033 [1; 32; 40 m] \ 033 [1; 31; 40m \ $ \ 033 [1; 32; 40 m \]"

The outermost "\ [\]" is used to enclose the character strings of the escape sequence and prevent the text of the escape sequence from occupying too much space in shell.

\ 033 declares the start of the escape sequence, and then [begins to define the color. The following 0 defines the default font width, followed by a number in the middle to define the character color. The last digit defines the character background color. The letter m is necessary for definition, and the character after the letter m is the character you want to change.

Optional colors: red, green, yellow, blue, magenta, cyan, and white. Their corresponding color codes are: 30 (black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (foreign Red), 36 (blue), 37 (white ).

Set the background color using the same color method, but replace the first digit "3" with "4", for example, 40, 41, 42, 43, 44, 45, 46, and 47.

For example, in my definition above: The final result is that all the characters before $ are expanded with a light green middle arc. The $ character is displayed in red. The subsequent input is highlighted in light green. In the arc, the user name @ host is displayed as dark green, and the path is displayed as foreign red.

Use code to explain:

Ps1 = "\ [-------------> outermost arc

\ 033 [1; 32; 40 m [

----> Define the rightmost "[";

1: font width, which seems to be highlighted.

32: character color. 32 indicates green.

40: background color. 40 indicates black.

\ 033 [0; 32; 40m \ u @ \ h:

---------> Define the \ u @ \ h on the right: these character colors.

0: default font width.

32: character color. Green.

40: background color. Black.

\ 033 [1; 35; 40 m \ w

---------> Define \ w (path color) on the right ).

1: character width. Highlight.

35: character color. Yang Hong.

40: background color. Black.

\ 033 [1; 32; 40 m]

---------> Define] characters on the right. Use the same effect as the preceding [character.

\ 033 [1; 31; 40m \ $

---------> The $ character is defined as red. Escape characters, so \.

The colors are similar to those in the front. I believe everyone has discovered the rule.

\ 033 [1; 32; 40 m

----------> This defines the color that you want to enter in the command line.

\]"

-----------> The string ends the arc.

This is the detailed definition of the above example.

In fact, I did not turn off the color escape sequence, so that I could change the color of the characters I entered. it's cool to watch this black and white screen all day !! If you do not want to change the input color, use \ 033 [0 m to close the escape sequence. \ 033 [0 m is the default color of shell. In the above example, add \ 033 [0 m before.

 

 

Note: You can also copy the. bash * files of other users to your home directory. This is what we do.

Related Article

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.