MAC Terminal termial highlighting configuration (PS1 variable configuration)

Source: Internet
Author: User
Tags word wrap

Operating Environment:

    • System: Mac 10.12
    • Editor: Vim

One, no brain configuration:

1. Open the Midrange input:

VI ~/.bash_profile

2. Open and edit the. bash_profile file:

Press keyboard "I" to enter edit mode

Copy the following code:

Export clicolor=1export PS1="

Press esc--"W" and press "Q"--"restart terminal--" complete!

Second, advanced operation:

Escape characters:

The above example uses a backslash-escaped sequence of characters to notify Bash to replace them with a specific value, and bash's recognizable escape sequence has the following:

  • \u User Name
  • \h hostname The first part
  • \h Hostname Full Name
  • \w current working directory (e.g. "/home/username/mywork")
  • \w "base name (basename)" For the current working directory (such as "mywork")
  • \ t 24-hour system time
  • \ t 12-hour system time
  • \@ 12-hour time with AM/PM
  • \d "Sat Dec 18″ format Date
  • \s the name of the shell (such as "bash")
  • \v bash version (e.g. 2.04)
  • \v Bash version (including patch level)
  • \ n line break
  • \ r return character
  • \ \ counter Slash
  • \a ASCII Bell character (you can also type 07)
  • \e ASCII Escape character (you can also type 33)
  • \[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.

Knowing all the escape sequences, you can customize your own terminal prompt.
Color configuration:
After we have designed the prompt, we can add color to them and add a color that adds a private escape sequence that the terminal (rather than bash) recognizes, so that it displays portions of the text in color. Standard Linux terminals and X terminals allow you to set the foreground (text) color and background color and, if necessary, enable the "bold" character. There are eight colors that we can choose from (in numbers).

    • ———————
    • Foreground background color
    • ———————
    • 30 40 Black
    • 31 41 Red
    • 32 42 Green
    • 33 43 Yellow
    • 34 44 Blue
    • 35 45 Purplish red
    • 36 46 Cyan Blue
    • 37 47 White

    • ————————
    • Code meaning
    • ————————
    • 0 OFF
    • 1 highlighting
    • 4 Underline
    • 5 flashes #闪烁这个选项在我机子上没反应, so there's no verification, don't know if it can ha
    • 7 Anti-white display
    • 8 Not visible

Example:
Now we can add color to the prompt based on the color table above:
the following definitions:
ps1= "\w>"
into:
ps1= "\e[32;40m\w>"
So far, the cue line has been pretty good, but it's still not perfect. After bash shows the working directory, we need to use the "\e[0m" sequence to reset the color to normal.
ps1= "\e[32;40m\w> \e[0m"
This definition will show a nice green cue line, but still need to do some cleanup work. Sometimes we don't need to include the background color setting of "Max" because it sets the background to black and black is the default color. In addition, Green is still very dark; we fix this problem by adding a "1" color code, which will enable brighter bold text. In addition to this modification, all nonprinting characters need to be enclosed in a dedicated bash escape sequence "\[" and "\ ". These two sequences inform bash that the enclosed characters do not occupy any space on the line, so that the word wrap continues to function properly. There are no two escape sequences, although we have a very nice hint line, but if you type the command exactly to the right end of the terminal, it will cause the display to be confusing. The following is the final prompt line:
ps1= "\[\e[32;1m\]\w> \[\e[0m\]"
we don't have to worry about using several colors in the same cue line, like this (my color configuration):
ps1=\[\e[36;1m\]\[email protected]:\[\e[32;1m\]\w\[\e[36;1m\]> \[\e[0m\]
However, when we write this configuration to the. bashrc file, we will find a problem, that is, Terminal's title bar has become an immutable "termianl", which may not be what we want, the default display user name, Hostname and current working path (I prefer this default ha), we may want to bring him back. In fact, the configuration title bar and the above mentioned configuration prompt content is similar, just to add:
\e]2;...\a
The middle ellipsis can add what we want the title bar to display.
For example we put "\e]2; This is my terminal:>\a " added to the PS1 variable (which of course also needs to be enclosed in " \[" and " \] " escape sequences), and the title bar will show this is my terminal:>
I prefer the default way ha, so I added the "\e]2;\[email protected]\h:\w\a", the final configuration completed the result is:
ps1= "\[\e]2;\[email protected]\h:\w\a\]\[\e[36;1m\]\[email protected]:\[\e[32;1m\]\w\[\e[36;1m\]> \[\e[0m\ ] "

MAC Terminal termial highlighting configuration (PS1 variable configuration)

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.