这是我的当前PS1:
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
我怎样才能以不同的颜色显示当前分支?
这是我的当前PS1:
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
我怎样才能以不同的颜色显示当前分支?
这里是分步介绍(不包括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
看起来是这样的(使用我的终端配色):
\e[0;32m
- 设置颜色(在此示例中为绿色)
\e[m
- 将颜色设置回默认设置$
:export PS1='\e[0;32m\w\e[m $'
还有其他颜色可供选择。请查看这篇文章的"colorization"部分,以获得全面的备选列表。
\[
和\]
之间进行包装,否则如果bash计算字符数不正确,可能会出现命令行过长而无法正确换行的问题。参考链接:http://askubuntu.com/questions/24358/how-do-i-get-long-command-lines-to-wrap-to-the-next-line - Kjir$(tput setaf 2)
(2表示绿色,1表示红色,3表示黄色等),$(tput bold)
表示粗体,$(tput sgr0)
表示重置。请参见https://mywiki.wooledge.org/BashFAQ/037。如果分配给`PS1`,请使用建议的`\[`和`\]`:https://mywiki.wooledge.org/BashFAQ/053。 - MestreLionexport PS1='\[\e[0;32m\]\w\[\e[0m\] $ '
- MhagnumDw这是我的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$
function pc {
[ -d .git ] && git name-rev --name-only @
}
PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '
.git
成为一个文件,因此对于某些情况来说,使用 [ -r .git ]
可能会有所改善。 - danblack这是我的PS1方案。
在Mac上使用Novel主题时看起来很棒。 抱歉,我的缩进有点混乱。您可以随意调整。
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"
如果你在一个分支上,会显示分支的名称。\
转义 $
。 - Gauthierpython3 -m pip install powerline-status
)。我想我喜欢它! - harperville将它放在你的 .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'
快速技巧:
~/.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\]$ "
source ~/.bashrc
:更多详情请查看:https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745
对于我的Homebrew主题的Mac,这非常有效。经过充分调试,速度非常快,并且完全自包含。 额外奖励:只有在您实际进入git repo时才聪明地以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 "
看一下liquidprompt:
https://github.com/nojhan/liquidprompt
也许对你的要求有点过重,但是你可以通过设置 LP_ENABLE_...=0
来关闭功能。
请参阅上述页面上的文档。
这是@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://dev59.com/cm855IYBdhLWcg3wz3zo#4138531
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 用户时会显示 #
。
local __git_dirty='`git rev-parse 2>/dev/null && (git diff --no-ext-diff --quiet --exit-code 2> /dev/null || echo -e \*)`'
- Ungluedgit rev-parse --abbrev-ref HEAD
- mikezter