cfi*_*her 55 git bash command-prompt
这是我目前的PS1:
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
Run Code Online (Sandbox Code Playgroud)
如何以不同颜色显示当前分支?
sha*_*sir 116
这是,部分(并没有Ruby):
function color_my_prompt {
local __user_and_host="\[\033[01;32m\]\u@\h"
local __cur_location="\[\033[01;34m\]\w"
local __git_branch_color="\[\033[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="\[\033[35m\]$"
local __last_color="\[\033[00m\]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt
Run Code Online (Sandbox Code Playgroud)
看起来像这样(用我自己的终端调色板):

Sim*_*ker 34
您可以使用以下内容包装所需的颜色部分:
\e[0;32m - 设置颜色(在这种情况下,为绿色)
\e[m - 将颜色设置回默认值
例如,这会将提示设置为当前路径的最后一个标记(绿色),然后$是默认颜色:
export PS1='\e[0;32m\w\e[m $'
Run Code Online (Sandbox Code Playgroud)
其他颜色也可以.有关替代品的完整列表,请查看着色下的这篇文章.
cmc*_*nty 22
这是我的PS1系列:
\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$
Run Code Online (Sandbox Code Playgroud)

小智 17
function pc {
[ -d .git ] && git name-rev --name-only @
}
PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '
Run Code Online (Sandbox Code Playgroud)

小智 12
这是我的PS1解决方案.
它在具有小说主题的Mac上看起来很棒.对不起,但我的缩进得到了一点点.劈开它直到你喜欢它.
function we_are_in_git_work_tree {
git rev-parse --is-inside-work-tree &> /dev/null
}
function parse_git_branch {
if we_are_in_git_work_tree
then
local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
if [ "$BR" == HEAD ]
then
local NM=$(git name-rev --name-only HEAD 2> /dev/null)
if [ "$NM" != undefined ]
then echo -n "@$NM"
else git rev-parse --short HEAD 2> /dev/null
fi
else
echo -n $BR
fi
fi
}
function parse_git_status {
if we_are_in_git_work_tree
then
local ST=$(git status --short 2> /dev/null)
if [ -n "$ST" ]
then echo -n " + "
else echo -n " - "
fi
fi
}
function pwd_depth_limit_2 {
if [ "$PWD" = "$HOME" ]
then echo -n "~"
else pwd | sed -e "s|.*/\(.*/.*\)|\1|"
fi
}
COLBROWN="\[\033[1;33m\]"
COLRED="\[\033[1;31m\]"
COLCLEAR="\[\033[0m\]"
# Export all these for subshells
export -f parse_git_branch parse_git_status we_are_in_git_work_tree pwd_depth_limit_2
export PS1="$COLRED<$COLBROWN \$(pwd_depth_limit_2)$COLRED\$(parse_git_status)$COLBROWN\$(parse_git_branch) $COLRED>$COLCLEAR "
export TERM="xterm-color"
Run Code Online (Sandbox Code Playgroud)
如果您在分支机构签出,则会获得分支机构名称.
如果你在一个刚刚开始的Git项目中,你就得到'@'.
如果你是无头的,你会得到一个相对于某个分支或标签的漂亮的人名,在名字前面加一个'@'.
如果你是无头而不是某个分支或标记的祖先,那么你就得到简短的SHA1.
另外,红色" - "表示干净的工作目录和索引,红色"+"表示相反.
小智 9
把它放在你的.bashrc或更好:将它保存在/ etc/bash-prompt中并从你的.bashrc中获取它.
使用tput应该是做颜色的正确方法.
#!/bin/bash
set_prompt()
{
local last_cmd=$?
local txtreset='$(tput sgr0)'
local txtbold='$(tput bold)'
local txtblack='$(tput setaf 0)'
local txtred='$(tput setaf 1)'
local txtgreen='$(tput setaf 2)'
local txtyellow='$(tput setaf 3)'
local txtblue='$(tput setaf 4)'
local txtpurple='$(tput setaf 5)'
local txtcyan='$(tput setaf 6)'
local txtwhite='$(tput setaf 7)'
# unicode "?"
local fancyx='\342\234\227'
# unicode "?"
local checkmark='\342\234\223'
# Line 1: Full date + full time (24h)
# Line 2: current path
PS1="\[$txtbold\]\[$txtwhite\]\n\D{%A %d %B %Y %H:%M:%S}\n\[$txtgreen\]\w\n"
# User color: red for root, yellow for others
if [[ $EUID == 0 ]]; then
PS1+="\[$txtred\]"
else
PS1+="\[$txtyellow\]"
fi
# Line 3: user@host
PS1+="\u\[$txtwhite\]@\h\n"
# Line 4: a red "?" or a green "?" and the error number
if [[ $last_cmd == 0 ]]; then
PS1+="\[$txtgreen\]$checkmark \[$txtwhite\](0)"
else
PS1+="\[$txtred\]$fancyx \[$txtwhite\]($last_cmd)"
fi
# Line 4: green git branch
PS1+="\[$txtgreen\]$(__git_ps1 ' (%s)')\[$txtwhite\]"
# Line 4: good old prompt, $ for user, # for root
PS1+=" \\$ "
}
PROMPT_COMMAND='set_prompt'
Run Code Online (Sandbox Code Playgroud)
~/.bashrc:parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]$(parse_git_branch)\[\e[00m\]$ "
Run Code Online (Sandbox Code Playgroud)
~/.bashrc:更多详细信息:https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745