PS1线与Git当前分支和颜色

时间:2010-11-09 13:01:03

标签: git command-prompt bash

这是我目前的PS1:

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

如何以不同颜色显示当前分支?

14 个答案:

答案 0 :(得分:111)

这是,部分(并没有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

看起来像这样(使用我自己的终端调色板):

Colored prompt

另请参阅thisthis文章。

答案 1 :(得分:33)

您可以使用以下内容包装所需的颜色部分:

\e[0;32m - 设置颜色(在本例中为绿色)

\e[m - 将颜色设置回默认值

例如,这会将提示设置为当前路径的最后一个标记(绿色),然后是默认颜色的$

export PS1='\e[0;32m\w\e[m $'

其他颜色也可用。有关替代品的完整列表,请查看着色下的this article

答案 2 :(得分: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$

alt text

答案 3 :(得分:16)

function pc {
  [ -d .git ] && git name-rev --name-only @
}
PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '

ps1

Source

答案 4 :(得分:11)

这是我的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"

如果您在分行办理结账,则会获得分行名称。

如果你是一个刚刚开始的Git项目,你就得到'@'。

如果你是无头的,你会得到一个相对于某个分支或标签的漂亮的人名,在名字前面加上一个“@”。

如果你是无头而不是某个分支或标记的祖先,那么你就得到简短的SHA1。

此外,红色的“ - ”表示干净的工作目录和索引,红色的“+”表示相反的情况。

答案 5 :(得分:8)

我超级强大的多行Linux提示!

将它放在你的.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'

答案 6 :(得分:3)

对于我的带有Homebrew主题的Mac,这非常有效。完全调试,非常快速,完全独立。 奖励:足够智能,只有当你真正 git回购中时,才会显示git分支作为提示的一部分! :)

# Color prompt for git
reset=$(tput sgr0)
boldgreen=$(tput setaf 2)$(tput bold)
cyan=$(tput sgr0)$(tput setaf 6)
boldred=$(tput setaf 1)$(tput bold)
boldwhite=$(tput setaf 7)$(tput bold)
boldyellow=$(tput setaf 3)$(tput bold)

PARENCLR=$'\001\e[0;36m\002'
BRANCHCLR=$'\001\e[1;33m\002'

alias branchname="git branch 2>/dev/null | sed -ne 's/^* \(.*\)/ ${PARENCLR}(${BRANCHCLR}\1${PARENCLR}\)/p'"

GIT_STATUS='$(branchname)'

PROMPT_CHAR="\$"
PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

这是它的样子: Mac + Homebrew + Color Git Prompt

如果您想拥有完整路径(或删除... /),只需将-W更改为-w(并删除... /)。

答案 7 :(得分:2)

只需使用适当的参数调用tput即可。请参阅tput(1)terminfo(5)手册页。

答案 8 :(得分:2)

看看liquidprompt:

https://github.com/nojhan/liquidprompt

可能有点太重,无法满足您的要求,但您可以通过设置

来关闭功能

LP_ENABLE_...=0

请参阅上页的文档。

答案 9 :(得分:2)

@cmcginty提示的修改后的版本,它添加了git解析函数,并且使用了稍有不同的间距:

# So I know where I am in repos:
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# Modified from:
# https://stackoverflow.com/a/4138531/2662028
export 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\]$(parse_git_branch " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n\$ '

这还将在提示符中使用\$而不是$,这意味着您在root用户时将得到#

答案 10 :(得分:1)

这是一个Windows / Cygwin / Bash解决方案。

将以下内容添加到~/.bashrc文件中。

xxx是您本地Git存储库的位置。

GetBranch()
{
    cat /cygdrive/c/xxx/.git/HEAD | sed 's+^ref: refs/heads/++'
}
export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(GetBranch) \[\e[33m\]\w \[\e[0m\] \n\$ "

答案 11 :(得分:1)

这是我的

export PS1="\n\[\033[1;30m\][$$:$PPID - \j:\!\[\033[1;30m\]]\[\033[0;36m\] \T \
\[\033[1;30m\][\[\033[1;34m\]\u@\H\[\033[1;30m\]:\[\033[0;37m\]${SSH_TTY:-o} \
\[\033[0;32m\]+${SHLVL}\[\033[1;30m\]] \[\033[1;37m\]\w\[\033[0;37m\]\[\033[1;34m\]\$(__git_ps1 \" (%s)\") \[\033[0;37m\] \n\$ "

答案 12 :(得分:0)

对于更复杂的Git状态,您可以使用some larger script

答案 13 :(得分:0)

快速破解:

  1. 将此添加到~/.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\]$ "
  1. 重新启动终端或源~/.bashrc

enter image description here

更多详细信息:https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745

相关问题