[英]PS1 line with Git current branch and colors
這是我目前的 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 $'
也可以使用其他顏色。 請查看着色下的本文,以獲取替代品的完整列表。
這是我的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$ '
這是我的PS1解決方案。
在帶有Novel主題的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"
如果您在分支機構中簽出,則會得到分支機構的名稱。
如果您處於一個init'd Git項目中,則只會得到“ @”。
如果沒有頭,您會得到一個相對於某個分支或標簽的好聽的人名,名稱前帶有“ @”。
如果您無頭且不是某個分支或標簽的祖先,則只需獲得短SHA1。
此外,紅色的“-”表示工作目錄和索引干凈,紅色的“ +”表示相反。
將其放在.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'
對於我的帶有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 +彩色Git提示
如果要使用完整路徑(或刪除... /),則只需將-W更改為-w(然后刪除... /)。
看一下液體提示:
https://github.com/nojhan/liquidprompt
可能對於您的要求來說太重了,但是您可以通過設置來關閉功能
LP_ENABLE_...=0
請參閱上一頁的文檔。
只需使用適當的參數調用tput
。 請參見tput(1)
和terminfo(5)
手冊頁。
@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用戶時將獲得#
。
這是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\$ "
這是我的
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狀態,您可以使用一些較大的腳本 。
快速破解:
~/.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
: 更多詳細信息: https : //medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745
這個 PS1 會將你當前的 git 分支塗成黃色:
導出 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 字符串(顯然其他方式也是可能的),請在此處鏈接到一個非常方便的 PS1 字符串生成器 for your.bashrc。 我將它用於上面的字符串:
它以一種簡單的方式解決了您的問題,更普遍地解決了構建自定義和彩色 shell 提示的問題,包括當前的 git 分支
看看這個項目信息欄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.