【发布时间】:2012-03-31 19:23:27
【问题描述】:
我更新了我的 .bashrc 文件如下:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$'
只要找到就可以了,我可以在提示中看到我的分支名称。但是,当我运行“屏幕”时,我得到了
“-bash: __git_ps1: 找不到命令”
这可能是什么原因?
【问题讨论】:
我更新了我的 .bashrc 文件如下:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$'
只要找到就可以了,我可以在提示中看到我的分支名称。但是,当我运行“屏幕”时,我得到了
“-bash: __git_ps1: 找不到命令”
这可能是什么原因?
【问题讨论】:
root:~/project# -> root:~/project(dev)#
将以下代码添加到 ~/.bashrc 的末尾
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt
【讨论】:
如果你没有 __git_ps1 你可以使用
git branch --contains HEAD 2>/dev/null
它的显示与 __git_ps1 相同。
如果你创建这样的别名:
alias __git_ps1='git branch --contains HEAD 2>/dev/null'
例如您使用此命令得到的提示:
$PS1='[\u@\h \W(`__git_ps1`)]\$'
或与
PS1='[\u@\h \W\[\033[36m\](`__git_ps1`)\[\033[0m\]]\$'
如果你喜欢颜色
您使用 __git_ps1 的脚本和您的 promt 将完美运行。
【讨论】:
这是在 debian/ubuntu 上测试的。
bash-completion包~/.bashrc 中存在以下行并且未被注释掉。if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
【讨论】:
我发现修改现有提示比定义新提示更简洁。以下 sn -p 将 git 分支名称添加到现有提示符(即 $PS1)。可以在 ~/.bashrc 文件中添加如下 sn -p:
source /etc/bash_completion.d/git (for Ubuntu 12.04 or less)
source /etc/bash_completion.d/git-prompt (for Ubuntu 13.04 and higher)
PS1=$PS1'$(__git_ps1 "(%s) ")'
如果您想让分支名称为彩色,您也可以这样做: 例如,绿色定义为 [\e[0;32m]。我们将它添加到 git_ps1 函数的内部字符串中,然后使用 \e[0m 重置颜色。需要转义的括号来指示插入了“特殊”字符。
PS1=$PS1'$(__git_ps1 "\[\e[0;32m\](%s) \[\e[0m\]")'
许多其他颜色定义can be found here
【讨论】:
# Add following line to /.bashrc to show Git branch name in ssh prompt
PS1='\[\033[0;31m\]\w\[\033[0;33m\]$(__git_ps1)\[\e[0m\]$ '
\[\033[0;31m\] 是红色的
\[\033[0;33m\] 是黄色的
\[\e[0m\] 正常
【讨论】:
This blog post 解释说,您必须先添加行source /etc/bash_completion.d/git,然后才能使用__git_ps1。
这里是完整的例子:
source /etc/bash_completion.d/git
export PS1='\w$(__git_ps1 "(%s)") > '
这也启用了分支的自动完成。
使用该格式,您的提示将类似于(不着色):
~/my-repo(master) >
【讨论】:
/etc/bash_completion.d/git-prompt(这反过来又获取 /usr/lib/git-core/git-sh-prompt)而不是 /etc/bash_completion.d/git。 (您可能需要四处寻找正确的完成文件以获取源...)
问题是 bash 需要作为登录 shell 运行,才能在默认的 cygwin 设置中使用此功能。如果您在 cygwin bash 中运行 bash,您将遇到同样的问题。要将 screen 设置为在登录模式下运行 bash,请将此行添加到您的 ~/.screenrc 文件中:
shell -bash
【讨论】:
在.bashrc 中添加source ~/.bash_profile。
遇到了同样的问题,它对我有用。
【讨论】: