Magic prompt-enhance system prompt line

Source: Internet
Author: User
Tags printable characters

If you can easily make shell prompt lines colorful and contain more information, why should you stick to the monotonous standard shell prompt line? In this article, Daniel Robbins explains how to obtain the shell prompt line that meets your needs and how to dynamically update the title bar of the X terminal.

As a Linux/UNIX person, we have been working in shell for a long time, and in many cases, the following line is always staring at our prompt line:

bash-2.04$

If you happen to be a Super User (root), you have the right to use the following prompt line version that marks "Identity:

bash-2.04#

These prompt lines are not very beautiful. It is no wonder that some Linux versions have updated the default prompt line and added color and more information. However, even if you happen to have a new version with a good color line, it cannot be perfect. You may want to add or change several colors or add or delete some information in the prompt line. It is not difficult to design your own colorful and decorated prompt lines from the beginning.

Basic prompt line
In bash, you can change the value of the PS1 environment variable to set the prompt line, as shown below:

$ export PS1="> ">

The change will take effect immediately. by placing the "export" definition in your ~ The/. bashrc file can fix this change. As long as you want, PS1 can contain any number of plain text:

$ export PS1="This is my super prompt > "This is my super prompt >

Although this is interesting, it is not particularly useful to include a large amount of static text in the prompt line. Most customized prompt lines contain information such as the user name, working directory, or host name. These highlights help you navigate the shell world. For example, the following prompt line shows your username and Host Name:

$ export PS1="u@H > "drobbins@freebox >

This prompt line is especially useful for those who log on to multiple machines with multiple accounts with different names, because it reminds you of the permissions on which machine you are currently operating.

In the above example, we use a dedicated Character Sequence escaped with a backslash to notify bash to insert the user name and host name into the prompt line, when these escape character sequences appear in the PS1 variable, bash replaces them with specific values. We use the sequence "u" (indicating the user name) and "H" (representing the first part of the host name ). The following is a complete list of all the specific sequences that bash can recognize (you can find this list in the "PROMPTING" section of bash man page ):

Sequence Description
A ASCII Bell character (you can also type 07)
D Date in wed Sep 06 format
E ASCII escape characters (you can also type 33)
H The first part of the Host Name (such as "mybox ")
H Host Name (for example, "mybox.mydomain.com ")
J In this shell, the number of processes suspended by pressing ^ Z
L Terminal Device Name of this shell (for example, "ttyp4 ")
Line Break
Carriage Return
S Shell Name (such as "bash ")
24-hour time (for example, "23:01:01 ")
T 12-hour time (for example, "11:01:01 ")
@ 12-hour time with AM/PM
U User Name
V Bash version (such as 2.04)
V Bash version (including Patch Level )? /TD>
W Current working directory (for example, "/home/drobbins ")
W "Basename" (for example, "drobbins") of the current working directory ")
! Position of the current command in the History buffer zone
# Command number (as long as you type the content, it will accumulate at each prompt)
$ If you are not a Super User (root), insert a "$"; if you are a super user, a "#" is displayed "#"
Xxx Insert an ASCII character that is represented by a three-digit number xxx (with zero instead of an unused number, such as "07 ")
Backslash
[ This sequence should appear before the Character Sequence without moving the cursor (such as the color escape sequence. It enables bash to correctly calculate the line feed.
] This sequence should appear after the non-printable character sequence.

In this way, you have learned all the special sequences escaped by backslash in bash. Please drill down these sequences to gain some perceptual knowledge about them in the way they work. After some tests, add the color below.

Colorization
It is quite easy to add colors. The first step is to design a prompt line without colors. Then, all we need to do is add a dedicated escape sequence that can be recognized by the terminal (rather than bash) so that it can display some parts of the text in color. Standard Linux terminal and X terminal allow you to set foreground (text) color and background color. If necessary, you can also enable the "bold" character. There are eight colors available for us to choose from.

Color is selected by adding a specific sequence in ps1. Basically, it is a numerical value between "e [" (escape square brackets) and "m. If more than one numeric code is specified, separate them with semicolons. The following is a color code example:

"e[0m"

If the numeric code is set to zero, it notifies the terminal to reset the foreground, background, and bold settings to their default values. You may use this code when the prompt line ends to make the text you typed non-color. Now let's take a look at these color codes.

To use this table, first find the color you want to use, and then find the corresponding foreground number (30-37) and background number (40-47 ). For example, you can set the numbers to 32 and 40, respectively, if you prefer black-green text. Then open your prompt line definition and add the appropriate color code in it. The following definition:

export PS1="w> "

Changed:

export PS1="e[32;40mw> "

So far, the prompt line is not perfect even though it is already good. After bash displays the working directory, we need to use the "e [0 m" sequence to reset the color to the normal value.

export PS1="e[32;40mw> e[0m"

This definition will display a nice green prompt line, but we still need to do some scanning work. We do not need to include the "40" background color settings because it sets the background to black, while black is the default color. In addition, the green color is quite dark. We can fix this problem by adding a "1" color code, which enables brighter bold text. In addition to this modification, we also need to include all non-printable characters in a dedicated bash escape sequence "[" and. The two sequences notify bash that the enclosed characters do not occupy any space on the row, so that the automatic line feed can continue to work normally. Without these two escape sequences, even though you have a very beautiful prompt line, if the command you typed happens to the rightmost end of the terminal, it will cause confusion. The following is our final prompt line:

export PS1="[e[32;1m]w> [e[0m]"

Don't worry about using several colors in the same prompt line, as shown below:

export PS1="[e[36;1m]u@[e[32;1m]H> [e[0m]"

Fun in Xterm
I have explained how to add information and colors in the prompt line, but you can proceed further. You can dynamically update the title bar of the X terminal (such as rxvt or aterm) by adding special code to the prompt line. All you have to do is add the following sequence to your PS1 prompt line:

"e]2;titlebara"

You only need to replace the substring "titlebar" with the text you want to appear in the xterm title bar. Now everything is ready! You do not need to use static text. You can insert the bash escape sequence into the title bar. In the following example, the user name, host name, and current working directory are displayed in the title bar and a brief, bright green prompt line is defined:

export PS1="[e]2;u@H wae[32;1m]>[e[0m] "

This is the prompt line I used in the above screenshot results. I like this prompt line because it displays all the information on the title bar rather than on the terminal. The terminal has a limit on the number of characters that a line can display. By the way, make sure to include the sequence of your title bar with "[" and "]" (because this sequence is not a print sequence for terminals ). The problem with storing a large amount of information in the title bar is that you cannot see this information if you are using a non-graphic terminal (such as the system console. To solve this problem, you can add the following lines in your. bashrc:

if [ "$TERM" = "linux" ]then    #we're on the system console or maybe telnetting in    export PS1="[e[32;1m]u@H > [e[0m]"else    #we're not on the console, assume an xterm    export PS1="[e]2;u@H wae[32;1m]>[e[0m] " fi

This bash Condition Statement dynamically sets the prompt line based on the current terminal settings. To achieve consistency, you must configure your ~ /. Bash_profile, so that it can search for your ~ /. Bashrc. Make sure that your ~ The/. bash_profile file contains the following line:

source ~/.bashrc

In this way, no matter whether you start a logon shell or a non-Logon shell, you will receive the same prompt line.

Now, you have mastered the magic tips. Now let's have a try and make a beautiful color prompt!

Reference resources

  • Rxvt is a good and small xterm, which happens to have a lot of documents about the escape sequence, which are concentrated in the "doc" directory in the tar package of the source code.
  • Aterm is another terminal program based on rxvt. It supports several good visualization features, such as transparency and coloring.
  • Bashish is a theme engine applicable to various terminals. Please check some good screen capture results of bashish at work!

Author Profile
Daniel Robbins lives in Albuquerque, New Mexico. He is the general carrier and CEO of Gentoo Technologies.Gentoo Linux(An advanced Linux running on a personal computer) andPortageThe creator of the system (next-generation Linux port system. He was also published by Macmillan.Caldera OpenLinux Unleashed,SuSE Linux UnleashedAndSamba UnleashedOne of the important authors of several books. Due to the deep influence of Pan Man, Daniel was fascinated by computers when he first caught up with the Logo programming language during his sophomore year. This may be becauseSONY Electronic Publishing/PsygnosisThe reason for the chief graphics artist. Daniel likes to spend good time with his wife Mary and his new daughter hadassia. You can contact Daniel through drobbins@gentoo.org.

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.