bash 带有 Git 当前分支和颜色的 PS1 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4133904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:51:15  来源:igfitidea点击:

PS1 line with Git current branch and colors

gitcommand-promptbash

提问by cfischer

Here is my current PS1:

这是我目前的 PS1:

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

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

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

采纳答案by Simon Whitaker

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

您可以使用以下方法包裹所需的颜色部分:

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

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

\e[m- sets colour back to the default

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

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;32m\w\e[m $'

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

其他颜色也可用。在着色下查看这篇文章,以获取完整的替代方案列表。

回答by shaman.sir

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

这是部分(并且没有 Ruby):

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

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

Colored prompt

彩色提示

Also, see thisand thisarticle.

另外,请参阅thisthis文章。

回答by cmcginty

Here is my PS1 line:

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

替代文字

回答by Steven Penny

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

ps1

ps1

Source

来源

回答by polypus74

This is my PS1 solution.

这是我的 PS1 解决方案。

It looks great on a Mac with the Novel theme. Sorry, but my indentation got munged a bit. Hack it till you like it.

它在具有小说主题的 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|.*/\(.*/.*\)||"
    fi
}

COLBROWN="\[3[1;33m\]"
COLRED="\[3[1;31m\]"
COLCLEAR="\[3[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"

If you are checked out at a branch, you get the branch name.

如果您在分行结账,您会得到分行名称。

If you are in a just init'd Git project, you just get '@'.

如果你在一个刚刚初始化的 Git 项目中,你只会得到“@”。

If you are headless, you get a nice human name relative to some branch or tag, with an '@' preceding the name.

如果你是无头的,你会得到一个相对于某个分支或标签的好听的人名,名字前面有一个“@”。

If you are headless and not an ancestor of some branch or tag you just get the short SHA1.

如果您是无头的并且不是某个分支或标签的祖先,您只会得到简短的 SHA1。

In addition, a red '-' signifies a clean work directory and index, and a red '+' signifies the opposite.

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

回答by cylgalad

My uber-powerful multi-line Linux prompt!

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

Put it either in your .bashrc or better: save it in /etc/bash-prompt and source it from your .bashrc.
Using tput is supposed to be the right way to do colors.

将它放在您的 .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='247'
   # unicode "?"
   local checkmark='243'
   # 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'

回答by Dan L

For my Mac with the Homebrew theme, this works really well. Fully debugged and very fast, and completely self-contained. BONUS: Smart enough to ONLY show a git branch as part of the prompt when you're actually ina git repo! :)

对于我的带有 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=$'
# So I know where I am in repos:
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}

# 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$ '
1\e[0;36m##代码##2' BRANCHCLR=$'##代码##1\e[1;33m##代码##2' alias branchname="git branch 2>/dev/null | sed -ne 's/^* \(.*\)/ ${PARENCLR}(${BRANCHCLR}${PARENCLR}\)/p'" GIT_STATUS='$(branchname)' PROMPT_CHAR="$" PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

Here's what it looks like: Mac + Homebrew + Color Git Prompt

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

If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../).

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

回答by ryanpcmcquen

Modified version of @cmcginty's prompt that adds in the gitparsing function and uses slightly different spacing:

@cmcginty's prompt 的修改版本,增加了git解析函数并使用了稍微不同的间距:

##代码##

This also uses \$in the prompt instead of $, which means you will get #when you are root.

这也在\$提示中使用而不是$,这意味着您将#在 root 时获得。

回答by botkop

Take a look at liquidprompt:

看一下liquidprompt:

https://github.com/nojhan/liquidprompt

https://github.com/nojhan/liquidprompt

Maybe a bit too heavy for your requirements, but you can switch features off by setting

可能对您的要求来说有点太重了,但是您可以通过设置关闭功能

LP_ENABLE_...=0

LP_ENABLE_...=0

See the documentation on above page.

请参阅上页的文档。

回答by Ignacio Vazquez-Abrams

Just invoke tputwith the appropriate parameters. See the tput(1)and terminfo(5)man pages.

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