小玩具 -- 教你改造終端命令列提示符

Newiep發表於2016-10-15

看過 laracast 視訊的同學應該都見過 jeffway 的命令列有一個雲朵:cloud: 和一個閃電 :zap:,其實是oh-my-zsh的一個主題 cloud,用 Mac 的同學可以看一下超哥以前寫的 Config Zsh On Osx。相信許多同學開發都是在虛擬機器下進行的,我喜歡多端統一樣式,所以今天說一下怎麼配置虛擬機器裡的命令列提示符(prompt)!

什麼會影響 bash 的命令列提示(prompt)?

Bash 配置它的命令列提示符主要使用兩個環境變數 PS1PS2。這裡有關於PS1PS2甚至PS3,PS4的詳解Bash Shell: Take Control of PS1, PS2, PS3, PS4 and PROMPT_COMMAND

簡單說一下 PS1PS2

  • PS1就是我們最常看到的,比如每次登陸伺服器時,我們輸入Linux命令的字首就是PS1所決定的。也是今天要說的重點。

  • PS2 是當命令太長,我們需要\換行來完成一條很長很長的命令時起作用的!比如,
☁  ~ ⚡  echo this is first line \
> this is second line \
> ...

☁  ~ ⚡  echo $PS2
>

看到沒,符號>就是PS2所定義的

  • PS3PS4請看連結瞭解詳細使用情況

有了修改的目標就好說啦!開啟家目錄下的.bashrc,找到PS1相關的行,

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='\[\033[1;36m\]☁  \[\033[1;32m\]\W \[\033[1;36m\]$(parse_git_branch)\]⚡  \[\033[0;37m\]'
else
    PS1='☁ \W ⚡ '
fi

# This function lets us know which branch we are on when working in a local repo:
parse_git_branch(){
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
}

unset color_prompt force_color_prompt

起關鍵作用的就是這裡啦。首先先註釋掉force_color_prompt=yes前面的#號,讓我們的客戶端有顏色。解釋一下PS1的值代表什麼意思:

  • \[\033[1;36m\]表示一種顏色,會對後面的輸出產生影響,顏色符號對照表看下面。其中\033是固定值,用來告訴bash使用八進位制的ASCI碼值來表示顏色。後面的1;36m分兩部分,1:加粗高亮,對應的還有0:普通的文字4:下劃線36就是對應的顏色碼值了,它代表Cyan 藍綠色。(m表示什麼暫時不知道了)下面還有其他的顏色碼值:
 Bash Color Codes:

  30: Black
  31: Red
  32: Green
  33: Yellow
  34: Blue
  35: Purple
  36: Cyan
  37: White
  • 後面的 ☁ 會在客戶端顯示一個雲的標誌(目前只在mac下測試過,不知道對window支援的怎麼樣),緊接著又是一個顏色的符號,它表示淡綠色

  • \W表示當前目錄名,對應的是\w表示當前目錄全路徑,下面還有其他一些符號表示:
  Bash variables: 
  \a : an ASCII bell character (07) 
  \d : the date in "Weekday Month Date" format (e.g., "Tue May 26") 
  \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required 
  \e : an ASCII escape character (033) 
  \h : the hostname up to the first '.' 
  \H : the hostname 
  \j : the number of jobs currently managed by the shell 
  \l : the basename of the shell’s terminal device name 
  \n : newline 
  \r : carriage return 
  \s : the name of the shell, the basename of $0 (the portion following the final slash) 
  \t : the current time in 24-hour HH:MM:SS format 
  \T : the current time in 12-hour HH:MM:SS format 
  \@ : the current time in 12-hour am/pm format 
  \A : the current time in 24-hour HH:MM format 
  \u : the username of the current user 
  \v : the version of bash (e.g., 2.00) 
  \V : the release of bash, version + patch level (e.g., 2.00.0) 
  \w : the current working directory, with $HOME abbreviated with a tilde 
  \W : the basename of the current working directory, with $HOME abbreviated with a tilde 
  ! : the history number of this command 
  # : the command number of this command 
  \$ : if the effective UID is 0, a #, otherwise a $ 
  \nnn : the character corresponding to the octal number nnn 
  \ : a backslash 
  [ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt 
  ] : end a sequence of non-printing characters
  • 後面比較特殊的就是$(parse_git_branch),因為我在下面定義了一個方法,用來獲取當前git的分支名稱,這裡就是呼叫這個方法,把分支名稱顯示到當前命令列提示符中。
    # This function lets us know which branch we are on when working in a local repo:
    parse_git_branch(){
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
    }

上面的就是我的配置,效果大概是下面這個樣子的

file

相關文章