PS1 line with git current branch and colors

Here is my current PS1

export PS1='[u@h W$(__git_ps1 " (%s)")]$ '

How can I display the current branch in a different color?


You can wrap the part that you want in colour with the following:

e[0;32m - sets colour (in this case, to green)

e[m - sets colour back to the default

For example, this sets the prompt to the last token of the current path, in green, followed by $ in the default colour:

export PS1='e[0;32mwe[m $'

Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.


Here is, part by part (and no ruby):

function color_my_prompt {
    local __user_and_host="[33[01;32m]u@h"
    local __cur_location="[33[01;34m]w"
    local __git_branch_color="[33[31m]"
    #local __git_branch="`ruby -e "print (%x{git branch 2> /dev/null}.grep(/^*/).first || '').gsub(/^* (.+)$/, '(1) ')"`"
    local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\* (.+)$/(\1) /`'
    local __prompt_tail="[33[35m]$"
    local __last_color="[33[00m]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt

Looks like this (with my own terminal palette):

Also, see this and this article


Here is my PS1 line:

n[e[1;37m]|-- [e[1;32m]u[e[0;39m]@[e[1;36m]h[e[0;39m]:[e[1;33m]w[e[0;39m][e[1;35m]$(__git_ps1 " (%s)")[e[0;39m] [e[1;37m]--|[e[0;39m]n$

链接地址: http://www.djcxy.com/p/19488.html

上一篇: 我如何删除已合并的所有Git分支?

下一篇: PS1线与git当前分支和颜色