当我';我在Git分公司?

当我';我在Git分公司?,git,macos,bash,iterm,Git,Macos,Bash,Iterm,我正在尝试以与相同的方式设置iTerm提示符 到目前为止,我在~/.profile中有以下内容: # Add git branch name to prompt parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/ on \1/' } PS1='\n\[\033[0:35m\]\u\[\033[0;32m\]\w\[033[0m\]$(parse_git_branch)\n\$\

我正在尝试以与相同的方式设置iTerm提示符

到目前为止,我在
~/.profile
中有以下内容:

# Add git branch name to prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/ on \1/'
}

PS1='\n\[\033[0:35m\]\u\[\033[0;32m\]\w\[033[0m\]$(parse_git_branch)\n\$\[\033[0m\] '
export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"
我不知道如何使树枝以不同的颜色出现,而不是前面的“开”

除此之外,还有其他功能,如:

  • 当不在git分支中时,在提示符处显示“o”
  • 在分支中显示“±”
  • 在行尾显示日期

任何帮助都将不胜感激

将此添加到您的
~/.bashrc
~/.profile

PS1="\u@\h:\w on\e[0;35m$(__git_ps1)\e[m\$ "
在哪里,

$(\uu git\u ps1)
用于打印分支名称

\e
定义配色方案的开始

[0;35m
表示紫色

\e[m
定义方案的结尾

另外,我修复了您当前的提示:

PS1='\n\[\033[0;35m\]\u\[\033[0;32m\]\w\[\033[0m\]$(__git_ps1)\n\$\[\033[0m\] '

与其使用古老的终端代码,不如使用
tput
,这使得代码更容易阅读,也更难搞乱:

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

# Set Titlebar and Prompt
TITLEBAR='\e]0;\h: ${PWD/$HOME/~}\a'
PS1="${TITLEBAR}${WHITE}[${POWDER_BLUE}\u@\h${WHITE}]${NORMAL}$ "

设置标题栏是可选的。只需确保在结尾使用
${NORMAL}
来关闭颜色更改。

我刚刚写了一篇关于如何完成所有这些的帖子。我已经介绍了所有的基础知识,但不得不猜测一些事情,例如Paul如何使用符号等。如果你想阅读,请查看

digitalformula.net上还有一篇文章展示了几个其他提示示例-请参阅

编辑: 代码部分如下:

PATH=$PATH:~/Data/Scripts:~/Data/Utils/rar:~/_Applications:~/_Applications/lynx

# alias to quickly show if any Handbrake processes are running
alias hb='sudo ps -aef | grep HandBrakeCLI'

# alias for quick DNS cache flushing
alias fc='sudo dscacheutil -flushcache'

# enable the git bash completion commands
source ~/.git-completion

# enable git unstaged indicators - set to a non-empty value
GIT_PS1_SHOWDIRTYSTATE="."

# enable showing of untracked files - set to a non-empty value
GIT_PS1_SHOWUNTRACKEDFILES="."

# enable stash checking - set to a non-empty value
GIT_PS1_SHOWSTASHSTATE="."

# enable showing of HEAD vs its upstream
GIT_PS1_SHOWUPSTREAM="auto"

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

# set the prompt to show current working directory and git branch name, if it exists

# this prompt is a green username, black @ symbol, cyan host, magenta current working directory and white git branch (only shows if you're in a git branch)
# unstaged and untracked symbols are shown, too (see above)
# this prompt uses the short colour codes defined above
# PS1='${GREEN}\u${BLACK}@${CYAN}\h:${MAGENTA}\w${WHITE}`__git_ps1 " (%s)"`\$ '

# this is a cyan username, @ symbol and host, magenta current working directory and white git branch
# it uses the shorter , but visibly more complex, codes for text colours (shorter because the colour code definitions aren't needed)
# PS1='\[\033[0;36m\]\u@\h\[\033[01m\]:\[\033[0;35m\]\w\[\033[00m\]\[\033[1;30m\]\[\033[0;37m\]`__git_ps1 " (%s)"`\[\033[00m\]\[\033[0;37m\]\$ '

# return the prompt prefix for the second line
function set_prefix {
    BRANCH=`__git_ps1`
    if [[ -z $BRANCH ]]; then
        echo "${NORMAL}o"
    else
        echo "${UNDERLINE}+"
    fi
}

# and here's one similar to Paul Irish's famous prompt ... not sure if this is the way he does it, but it works  :)
# \033[s = save cursor position
# \033[u = restore cursor position

PS1='${MAGENTA}\u${WHITE} in ${GREEN}\w${WHITE}${MAGENTA}`__git_ps1 " on %s"`${WHITE}\r\n`set_prefix`${NORMAL}${CYAN}\033[s\033[60C (`date "+%a, %b %d"`)\033[u${WHITE} '
我用

我以前的很多解决方案都只显示git分支,如果终端加载时我只在该目录中。如果我在非git repo中启动了iTerm,那么当我
cd
进入带有git repo的目录时,它就不起作用了


这个github项目为我解决了这个问题。

一个功能丰富且广泛的解决方案(不仅适用于iterm shell,也适用于Vim和其他应用程序)是。

如上所述,我还使用

运行此命令以快速安装:

mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git
将此添加到
~/.bash\u配置文件的顶部

# Add git branch name to prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/ on \1/'
}

PS1='\n\[\033[0:35m\]\u\[\033[0;32m\]\w\[033[0m\]$(parse_git_branch)\n\$\[\033[0m\] '
export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"
在同一个文件
~/.bash_profile
中,我使用以下提示:

export PS1="\n\[$txtpur\]\u\[$bldwht\]@\h\[$bldgrn\]:\[$bldblu\] \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ \[$txtwht\] "

export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w\$ "
你可以换个房间

以下是PS1中一些符号的含义:
\u-用户名
@-酷的符号
\h-主机名
:-区分事物的酷符号
\w-完整路径,使用\w表示短路径
\git_分行-当前分行的名称
\git_dirty-在分支发生更改时显示*
$-cool符号要表示,请输入命令


我建议您阅读本页以对提示进行着色,因为它也可以应用于Mac:如果您真的喜欢颜色:
PS1=“\[\033[01;32m\]\u\[\033[36m\]@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\e[1;35m$(\uu git\u PS1)\e[m\$”
我很确定您应该在这里使用单引号,而不是双引号。我是堆栈溢出新手,所以我想知道…在响应中包含代码而不是链接是协议吗?我希望人们阅读本文而不是在这里获取代码,因为在上面的代码正常工作之前,您需要做一些事情.@SiegeX想提及原始来源吗?@pablasso yea,me:)回顾这一点,我有点反应过度。如果提到颜色代码是我写的,我还是会很高兴的,但整个剽窃行为都是过度的,我收回了这一点。我对iTerm2的换行有问题,而且换行的日期也在最后,所以我把它洗牌了在%s上的目录/branch info
code
PS1='${MAGENTA}\u${WHITE}}{GREEN}\w${WHITE}${MAGENTA}
{u git_PS1}(
date“+%a,%b%d”
)\r\n
设置前缀
${NORMAL profix}{CYAN CYAN 033[60C\033]WHITE}“
code
这是Paul Irish的实际配置:这是他
源代码
d。bash_profile
:作为一个提示:可以为您完成整个git解析。它还支持不同的vcs系统。谢谢,这是迄今为止最简单的方法,应该更高级。使用其他解决方案时遇到一些问题,这确实是最简单的一个。Thanksi还有一个问题,当我在回购中更改分支时,提示不会更改,这就解决了这个问题。另外,设置/配置非常简单。谢谢分享。bill