How to modify the terminal font color and background color of Linux (ii)

Source: Internet
Author: User

Color settings under Linux terminal 2013-08-31 22:57:15

Category: LINUX

in ANSI-compatible terminals such as xterm, RXVT, Konsole, and so on, you can display text in color rather than just black and white. This article demonstrates how to use bold and colored text.

Trust every Linux user to face Bash. Although the first eye looks very dull, there are many ways to make your shell personalized enough. The color enhancement of the shell prompt makes your shell unique and easy to read.

In this article, I take the Bash shell as an example. Because the escape sequences in different terminals are not identical, I am using an ANSI terminal in this article.

The shell color configuration appears in Bash's personal profile ~/.BASHRC or in the global profile/ETC/BASHRC. You can set the appearance of the prompt by BASHRC the PS1 variable inside.

The general default is the case. BASHRC inside should be:

~/.bashrc

Export ps1= ' \[email protected]\h:\w\$ '

The above means: user @ Host: Current directory $

And this is a shell prompt that everyone is very familiar with.

The meanings of the commonly used parameters in the definition of PS1 are as follows:

\d:#代表日期, formatted as weekday month date, for example: "Mon-1"

\h:#完整的主机名称

\h:#仅取主机的第一个名字

\ t:#显示时间为24小时格式, such as: HH:MM:SS

\ t:#显示时间为12小时格式

\a:#显示时间为24小时格式: hh:mm

\u:#当前用户的账号名称

\v:#BASH的版本信息

\w:#完整的工作目录名称

\w:#利用basename取得工作目录名称, so only the last directory is listed

\#: #下达的第几个命令

\$:#提示字符, if it is root, the prompt is: #, the average user is: $

Escape sequences

If you add personalization by using a colored prompt, escape sequences will be used. An escape sequence is a control instruction that lets the shell perform a special step. Escape sequences usually start with the ESC (which is also why it is named). Represented in the shell as ^[. This notation takes a little time to get used to, or the same work can be done with \033 (the ESC ASCII code is in decimal notation is 27, equals 033 in octal).

To enter the escape sequence directly inside the shell we need to press Ctrl-v:ctrl-v ESC first.

Let's go to the topic below and say how to use the color of the shell. I use my own shell color settings for detailed instructions.

Code:

ps1= "\[\033[1;32;40m[\033[0;32;40m\[email protected]\h:\033[1;35;40m\w\033[1;32;40m]\033[1;31;40m\$\033[1;32; 40m \] "

The outermost "\[\" is to enclose the string of the escape sequence, preventing the text of the escape sequence from appearing in a shell that takes up too much space.

\033 declares the beginning of the escape sequence, and then [begins to define the color. The following 0 defines the default font width, followed by the middle number that defines the character color. The last digit defines the background color of the character. The letter M is necessary to define itself, and the character behind 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 (magenta), 36 (cyan), 37 (white).

Use the same color method to set the background color, but replace the first number "3" with "4", such as 40, 41, 42, 43, 44, 45, 46, 47.

For example, in my definition above: The last displayed result is that the characters before the $ number are all expanded in a light green bracket. The $ character is displayed in red. The input shown below is highlighted in light green. In parentheses, the user name @ host is shown in dark green and the path is magenta.

Explain in code:

PS1 = "\[-------------> outermost brackets

\033[1;32;40m[

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

1: Font width, as if there is a highlight function.

32: Character Color. 32 indicates green.

40: Background color, 40 indicates black.

\033[0;32;40m\[email protected]\h:

---------> Define \[email protected]\h on the right: these few character colors.

0: Default font width.

32: Character Color. Green.

40: Background color. Black.

\033[1;35;40m\w

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

1: Character width. Highlight

35: Character Color. Magenta.

40: Background color. Black.

\033[1;32;40M]

---------> Define the right] character. Use the same effect as the previous [character].

\033[1;31;40m\$

---------> defines the $ character as red. The escape character, so the front adds \.

The colors are almost the same as before. I believe we all find the law.

\033[1;32;40m

----------> This defines the color you want the command to be displayed on the command line.

\]"

-----------> String End Bracket.

This is the detailed definition of the above example.

In fact, I do not turn off the color escape sequence, let yourself enter the character also change the color, look at this black and white screen all day!!! If you do not want to change the input color, use \033[0m to close the escape sequence, \033[0m is the default color of the shell. In the example above, add \033[0m in front of \] ".

How to modify the terminal font color and background color of Linux (ii)

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.