Ubuntu Terminal color settings

Source: Internet
Author: User
Tags transparent color word wrap

The most enjoyable thing about Linux is that you can customize your system configuration to suit your personal preferences, like the terminal color setting is a typical example.

Figure 1 Terminal display in system default state
It's hard to get tired after working at a terminal without a custom configuration, because all of the input and output is a color, and it's inconvenient to find the execution results of some commands. In addition to the color, the long path name under the default terminal configuration is also a place where people compare egg aches. After some information on the Internet to find, only to know that the original configuration of the terminal, can be written to different files, such as: ~/.BASHRC,/ETC/BASH.BASHRC,/etc/profile these files. However, the roles of these files are different, and users must understand the timing of each file's action/etc/profile: This file sets the environment information for each user of the system, and the file is executed the first time the user logs on. And collects the shell's settings from the/ETC/PROFILE.D directory's configuration file. This file is called by default /ETC/BASH.BASHRCFile. /ETC/BASHRC: Executes this file for each user running the bash shell. When the bash shell is opened, the file is read. ~/.bash_profile: Each user can use the file to enter shell information dedicated to their own use, and when the user logs in, the file executes only once! By default, he sets some environment variables to execute the user's. bashrc file. ~/.BASHRC: This file contains bash information dedicated to your bash shell. ~/.bash_logout: Executes the file each time it exits the system (exiting the bash shell). After figuring out how several files relate to each other and how they function, we can understand that the configuration information should be written to the ~/.BASHRC file for the user's configuration. I personally think that the default terminal configuration has two disadvantages: 1. No different colors are used to highlight different content; 2. The path name is too long and sometimes the command you enter needs to be displayed on a new line. This article is accomplished by addressing both of these issues. 1. Color Configuration    In order to set the terminal color, we need to ~/.BASHRCIn the file PS1variable to be customized. First open the file with the "gedit ~/.BASHRC" command and find the location of the PS1 variable: Figure 2 PS1 variables in the. bashrc fileThese lines of code believe that it is not difficult to understand, simply said: In the case of color mode open, the PS1 variable represents the user name + hostname + path name (long path) + $. The following is a detailed analysis of the settings for the PS1 variable:

First we need to know the following tables:

Foreground background color


30 40 Black

31 41 Red

32 42 Green

33 43 Yellow

34 44 Blue

35 45 Purple Red

36 46 Blue

37 47 White

1 Transparent Color

Code meaning



1 highlighting

4 Underline

5 Flashing

7 Anti-white display

8 Not visible

Sequence description

\a ASCII Bell character (you can also type \007)

\d Date in "Wed Sep 06" format

\e ASCII escape character (you can also type \033)

\h The first part of the hostname (such as "Mybox")

Full name of the \h host (e.g. "mybox.mydomain.com")

\j The number of processes suspended in this shell by pressing ^Z

\l The terminal name of this shell (e.g. "TTYP4")

\ n line break

\ r return character

\s The name of the shell (such as "bash")

\ t 24-hour time (e.g. "23:01:01")

\ t 12-hour time (e.g. "11:01:01")

\@ 12-hour time with AM/PM

\u User name

\v bash version (e.g. 2.04)

\v Bash version (including patch level)?/td>;

\w Current working directory (e.g. "/home/drobbins")

\w "base name (basename)" For the current working directory (such as "Drobbins")

\!  Position of the current command in the history buffer

\# Command number (as long as you type, it will accumulate at each prompt)

\$ If you are not superuser (root), insert a "$"; if you are a superuser, display a "#"

\xxx Insert an ASCII character represented by a three-digit xxx (with 0 instead of unused digits, such as "/007")

\ \ Counter slash

\[ This sequence should appear before the character sequence that does not move the cursor (such as the color escape sequence). It enables bash to correctly calculate the wrapping.

\] This sequence should appear after the nonprinting character sequence.

After we have a basic understanding of the above knowledge, we can do the actual operation. The configuration file is backed up first using the command "CP ~/.BASHRC ~/.bashrc.backup" in the terminal (this reminds you, in Linux, if you need to make changes to the configuration file, it's best to make a backup, In case of a problem that causes the system to fail to log on after an error), then "Gedit ~/.BASHRC" opens the file, finds the code for the PS1 variable setting, and notes (inserts the comment symbol "#" before each line of code). At the end of the file, add "ps1= ' \[email protected]\h:\w\$ '" (This is the default setting for the terminal, i.e. no color):

After completing the above operation, save and exit the Gedit editor, go back to the terminal and enter "source ~/.BASHRC", found that there is no change in the place where the user information is displayed, indicating that the settings are consistent with the default terminal settings. Next, we need to set the font color of the displayed information, first look at how the color is set:

Setting formula for color

Color =\033[code; foreground; background m

For example: \033[1;32;40m indicates that the highlighted font is green and the background color is black (see the table earlier in this article for a colour definition)


To enclose all nonprinting characters in a dedicated bash escape sequence "\[" and "\ ". These two sequences inform bashthat the enclosed characters do not occupy any space on the line, so that the word wrap continues to function properly.

That's a key point, or else you'll have your terminal in a mess when you set the color.

Now the color of the upper PS1 variable is set, that is, where the color needs to change the color value, such as we have the user name in red Display command should be:

ps1= ' \[\033[1;31;40m\]\[email protected]\h:\w\$ '

Similarly, after modifying the. bashrc file, source, you should see the following results: In fact we do not want all the information to display the same color, so we have to find ways to show the information in different colors. Here you may notice that the function of the color setting is from the current setting command to the next color setting command, that is, if we only set the color before \u, then the terminal font should be red, just like below, not only the information item is red, even the input command is red:
To do this, we need to insert a new color value in front of the @ symbol to prevent the entire information item from appearing in red, and the modified PS1 variable is: ps1= ' \[\033[1;31;40m\]\u\[\033[00m\]@\h:\w\$ ' the same source, the results come out:
Having understood the principle of setting the PS1 variable, the user can set the PS1 variable as freely as he wants, such as inserting a string in a PS1 variable, or setting a different color for each information item, or controlling what the InfoBar displays: we can have the user name underlined, the path is shown in white, And the dollar sign is shown in green, the command is shown in blue: ps1= ' \[\033[4;31;40m\]\u\[\033[00m\]@\h:\[\033[37;40m\]\w\[\033[32;40m\]\$ \[\033[34;40m\] ' Source a bit, the effect is as follows:
At this point, the terminal color setting problem has been resolved, the following address the pathname problem. 2. Long path name problem in terminalSometimes the terminal needs to open a few layers of folders in order to access the files we need, and this time in the terminal to display the path name is like a long tail, is there any good way to make the terminal only display the user's current working directory name? In fact, we have mentioned in the preceding sequence description that we can display only the base name of the working directory in the PS1 variable setting terminal, and change the lowercase w of the \w in the PS1 variable to uppercase W, the modified PS1 variable is: ps1= ' \[\033[4;31;40m\]\u\[\033[00m\ ]@\h:\[\033[37;40m\]\w\[\033[32;40m\]\$ \[\033[34;40m\] ' The effect of the two is illustrated by a picture:

You can see that after modifying the value of the PS1 variable, the terminal will display only the base name of the current working directory. Finally, combined with the above, share my PS1 variable, the inside of my variable is set to not display the host name and show the short path name: ps1= ' ${debian_chroot:+ ($debian _chroot)}\[\033[01;04;32m\]\u\[\ 033[00m\]:\[\033[01;37m\]\w\[\033[31m\]\$ \[\033[00m\] '

Finally, you may also find that if you open a new terminal, the user-defined configuration is not displayed, but the default configuration is displayed.    At this point, you can manually load the. bashrc file by using the Source command, and the configuration we set up is back. Of course, the terminal as one of our common tools in the system, if every time you open the terminal to the source once, it is not the egg hurt dead? Go back to the beginning of the article where we say /etc/profileThe file collects the shell settings from the configuration file of the/ETC/PROFILE.D directory. This file is called by default /ETC/BASHRCFile, so we can open the profile file and take a look: "sudo gedit/etc/bashrc" (note the need to add sudo!)
As you can see, there is a code in the file that calls the/ETC/BASH.BASHRC file, so it can be inferred that the profile file that was described above automatically loads the BASH.BASHRC file, which is actually implemented by the code, so we can add a new line of code here as well. To enable profile to automatically load the user's. bashrc file, so that we do not need to open the terminal every time the need source.
After saving, close the editor, open a new terminal, you can find that our previous configuration has been able to automatically load.
3.ls Command SettingsAt the terminal a common command is the LS command, and we can implement some of the custom settings for the LS command by setting alias in the. bashrc file:
Alias is actually the equivalent of a command to refer to something, such as the last sentence, whenever you enter the LS command inside the terminal, the terminal will be based on the alias in the. bashrc file to find the referenced command: LS--color=auto, so you enter "LS" every time     In fact, the equivalent of the input "LS--color=auto", and the purpose of this command is to display the current directory of visible files, and color to distinguish between different types of files, such as folders and ordinary files. The last one is worth mentioning is a small suggestion, the previous view of my terminal is black, in fact, users can set preferences directly in the terminal to achieve a translucent background, or a picture as a background can also:

I use:

ps1='${debian_chroot:+ ($debian _chroot)}\[\033[00;00;32m\]\u\[\033[00m\]:\[\033[00;04;32m\]\w\[\ 033[00;01;34m\]\$ \[\033[00;00;36m\]'

Ubuntu Terminal color settings

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.