[英]PS1 line with Git current branch and colors
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?如何以不同颜色显示当前分支?
Here is, part by part (and no Ruby): 这是部分(而不是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
Looks like this (with my own terminal palette): 看起来像这样(使用我自己的终端面板):
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 article under colorization for a comprehensive list of alternatives. 请查看着色下的本文,以获取替代品的完整列表。
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$
This is my PS1 solution. 这是我的PS1解决方案。
It looks great on a Mac with the Novel theme. 在带有Novel主题的Mac上看起来很棒。 Sorry, but my indentation got munged a bit. 抱歉,但我的缩进有些微。 Hack it till you like it. 破解它,直到您喜欢为止。
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"
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 '@'. 如果您处于一个init'd 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. 此外,红色的“-”表示工作目录和索引干净,红色的“ +”表示相反。
Put it either in your .bashrc or better: save it in /etc/bash-prompt and source it from your .bashrc. 将其放在.bashrc或更高版本中:将其保存在/ etc / bash-prompt中,并从.bashrc中获取它。
Using tput is supposed to be the right way to do colors. 使用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'
For my Mac with the Homebrew theme, this works really well. 对于我的带有Homebrew主题的Mac,这确实很好。 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 in a git repo! 奖励:足够聪明,仅当您实际上在 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 "
Here's what it looks like: Mac + Homebrew + Color Git Prompt 外观如下: Mac + Homebrew +彩色Git提示
If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../). 如果要使用完整路径(或删除... /),则只需将-W更改为-w(然后删除... /)。
Take a look at 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
See the documentation on above page. 请参阅上一页的文档。
Just invoke tput
with the appropriate parameters. 只需使用适当的参数调用tput
。 See the tput(1)
and terminfo(5)
man pages. 请参见tput(1)
和terminfo(5)
手册页。
Modified version of @cmcginty's prompt that adds in the git
parsing function and uses slightly different spacing: @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\$ '
This also uses \\$
in the prompt instead of $
, which means you will get #
when you are root. 这还将在提示符中使用\\$
而不是$
,这意味着您在root用户时将获得#
。
Here is a Windows/Cygwin/Bash solution. 这是Windows / Cygwin / Bash解决方案。
Add the following to your ~/.bashrc
file. 将以下内容添加到~/.bashrc
文件中。
xxx is the location of your local Git repository. 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\$ "
Here's mine 这是我的
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\$ "
对于更复杂的Git状态,您可以使用一些较大的脚本 。
Quick hack: 快速破解:
~/.bashrc
: 将此添加到~/.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\]$ "
~/.bashrc
: 重新启动终端,或重新启动~/.bashrc
: More detail: https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745 更多详细信息: https : //medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745
This PS1 will color your current git branch in yellow:这个 PS1 会将你当前的 git 分支涂成黄色:
export PS1="[\033[38;5;11m]\u[$(tput sgr0)]@\h:[$(tput sgr0)][\033[38;5;6m][\w][$(tput sgr0)][\033[38;5;226m]($(git branch 2>/dev/null | grep '^*' | colrm 1 2))[$(tput sgr0)]: [$(tput sgr0)]"导出 PS1="[\033[38;5;11m]\u[$(tput sgr0)]@\h:[$(tput sgr0)][\033[38;5;6m][\w][$ (tput sgr0)][\033[38;5;226m]($(git branch 2>/dev/null | grep '^*' | colrm 1 2))[$(tput sgr0)]: [$(tput sgr0)]"
To see in a more interactive way the how the building of a PS1 string can be done (other ways are possible obviously), here the link to a very handy PS1 string generator for your.bashrc.要以更具交互性的方式查看如何构建 PS1 字符串(显然其他方式也是可能的),请在此处链接到一个非常方便的 PS1 字符串生成器 for your.bashrc。 I used it for the string above:我将它用于上面的字符串:
http://bashrcgenerator.com/ http://bashrcgenerator.com/
It solves in a simple way your question and more generally the issue of building of a customized and colorized shell prompt, including the current git branch它以一种简单的方式解决了您的问题,更普遍地解决了构建自定义和彩色 shell 提示的问题,包括当前的 git 分支
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.