Modify LINUX command prompt

Source: Internet
Author: User
Those who have used linux terminal will not feel unfamiliar with such a command prompt. in particular, they will be able to clearly know the host of their username in the multi-user status. However, there is a problem with it, that is, the directory is displayed at the same time. sometimes, when the directory is too long, it occupies the whole line, which is depressing. for example, when

Those who have used linux terminal will not be unfamiliar with such command prompts.

There is no problem with such a prompt, especially in the multi-user status, you can also clearly know the host of your user name.

However, there is a problem with it, that is, the directory is displayed at the same time. sometimes, when the directory is too long, it occupies the whole line, which is depressing, such

Of course, in a multi-user remote terminal, it is important to use the user name, host, and current directory. the proper method depends on your individual needs.

Now, I need to modify the command prompt in the single-user gnome-terminal. The user name and host do not need to be prompted at all, or simply display it together with the current directory on the title bar of gnome-terminal. In addition, some other features and colors can be added.

The ubuntu10.04 system is used as an example!

First, you need to know what is being controlled before you transform the command prompt (prompting. Take a look at man bash, search for prompt, and then you can see several variables like PS1, PS2, PS3, and PS4. Check whether the current system variables include echo $ PS1, echo $ PS2, and echo $ PS4 in my system.

The result of echo $ PS1 is

\ [\ E] 0; \ u @ \ h: \ w \ a \] $ {debian_chroot: + ($ debian_chroot)} \ u @ \ h: \ w \ $

The result of echo $ PS2 is

>

Echo $ PS4 returns

+

It is easy to see that PS1 is used to control the current command prompt, while PS2 is used to control the prompt when the command is not finished and the line feed is used. Here we only care about PS1. this string consists of many escape characters, which indicate what they mean. you can view infobash.

There are several escape characters that are obvious. \ U indicates user, \ h indicates hostname, and \ w indicates the current directory (abbreviated ~).

Let's take a look at $ {debian_chroot: + ($ debian_chroot)}. check that this is a conditional value assignment. If the system contains the debian_chroot variable, it will be added ($ debian_chroot), for example ~ /. Add exportdebian_chroot = "AAA" to bashrc. then, my command prompt will become

(AAA) user @ hostname-laptop ~ $

Debian_chroot is related to the/etc/debian_chroot file, which is neither defined in my system nor in the debian_chroot variable. Therefore, I can ignore this item.

In infobash, \ [and \] are a pair of delimiters used to limit non-printable characters. The potential meaning is not mentioned. since it is a non-printable character, why? It is not displayed anyway. In fact, there may be problems without limitation, because these non-printed characters will cause command prompt confusion. For example, some unfriendly conditions may occur when you exit from vim or use the direction key to view the command history. Therefore, this pair of delimiters is used to completely ignore these non-printable characters when the terminal determines the width of the command prompt. at the same time, it does not affect these non-printable characters.

In addition, it also means that non-printable characters must be placed in the middle of this pair of delimiters.

After knowing this, the \ e] 0; \ u @ \ h: \ w \ a symbol is left.

\ E] 0; titlebar \ a will display titlebar on the title bar, that is, \ e] 0; and the characters between \ a and constitute a non-print sequence, it is used to display these characters on the title bar of gnome-terminal.

The ASCII code of \ e is 033. it also plays an important role in color setting. If a person who has used BBS on a terminal should know this thing, the color on the terminal is controlled by it.

Specifically, the command for controlling the current color is placed between \ e [and m, and there are some numbers separated by semicolons. For example

\ E [32; 1 m indicates bright green.

32 indicates the color, and 1 indicates the depth of the color. In addition, \ e [0 M indicates that the original state is restored, and the subsequent text will not be affected by the previous color.

Therefore, the modified settings should be

Export PS1 = "\ [\ e] 0; \ u @ \ h: \ w \ a \ e [32; 1 m \] \ $ \ [\ e [0 M \]"

Note that there are two non-printable character sequences in the front and back.

In addition, sometimes it is useful to know the result code of the previous program running. the code is $ ?, Therefore, the above settings can be changed

Export PS1 = "\ [\ e] 0; \ u @ \ h: \ w \ a \ e [32; 1m \] $? \ $ \ [\ E [0m \]"

Appendix, ascii color control code (ANSI/3.64)

Foreground

Background color control

30 black

40

Black 0 close all properties

31 Red

41 Red 1 highlighted

32 Green

42 Green 4 single line

33 yellow

43 Yellow 5 flashing

34 Blue

44 Blue 7 reverse display

35 pink

45 Pink 8 hide

36 Lan

46 Lan

37 White

47 White

Therefore, each color control can be controlled by three numbers, for example, \ e [31; 45; 5 m indicates the blinking of the scarlet letter Foundation. Of course, Flash support requires a specific terminal. For gnome-terminal, the command

Printf "\ e [31; 45; 5 mABC"

There will be no flickering effect, but the color is there.

The color control in the ASCII code control command is provided above. in practical applications, for example, the display of BBS terminals also involves many other control commands, such as clearing the screen and setting the cursor. For details, refer to the relevant pages of some BBS sites, such as bdwm's asciiart version and Shui Mu's asciiart version.

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.