PS1 env变量在mac上不起作用

Jam*_*.Xu 10 git macros ps1

我有一个脚本(不是我自己编写的),它在我的命令提示符下显示了git branch/svn分支.有谁知道为什么这不适用于mac?它在linux中完美运行.

来自https://github.com/xumingming/dotfiles/blob/master/.ps1:

# Display ps1 with colorful pwd and git status
# Acording to Jimmyxu .bashrc
# Modified by Ranmocy
# --

if type -P tput &>/dev/null && tput setaf 1 &>/dev/null; then
    color_prompt=yes
else
    color_prompt=
fi

__repo () {
    branch=$(type __git_ps1 &>/dev/null && __git_ps1 | sed -e "s/^ (//" -e "s/)$//")
    if [ "$branch" != "" ]; then
        vcs=git
    else
        branch=$(type -P hg &>/dev/null && hg branch 2>/dev/null)
        if [ "$branch" != "" ]; then
            vcs=hg
        elif [ -e .bzr ]; then
            vcs=bzr
        elif [ -e .svn ]; then
            vcs=svn
        else
            vcs=
        fi
    fi
    if [ "$vcs" != "" ]; then
        if [ "$branch" != "" ]; then
            repo=$vcs:$branch
        else
            repo=$vcs
        fi
        echo -n "($repo)"
    fi
    return 0
}

if [ "$color_prompt" = yes ]; then
# PS1='\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[33;40m\]$(__repo)\[\e[00m\]\$ '
    PS1='\[\e[01;32m\]\u\[\e[00m\]:\[\e[01;34m\]\W\[\e[33m\]$(__repo)\[\e[00m\]\$ '
else
    PS1='\u@\h:\w$(__repo)\$ '
fi
unset color_prompt

case "$TERM" in
xterm*|rxvt*)
  PS1="\[\e]0;\W\a\]$PS1"
  ;;
*)
  ;;
esac
Run Code Online (Sandbox Code Playgroud)

Raf*_*cki 21

Mac的Mac OS X安装没有__git_ps1包含在内.

使用:

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"
Run Code Online (Sandbox Code Playgroud)

作为替代.

  • `\(*.*)/`hooray for grep` \(*.*)/` (4认同)
  • 事实上,[git-osx-installer](http://code.google.com/p/git-osx-installer/)附带`git-completion.bash`并将其安装到`/ usr/local/git的/的contrib /完成/`.只需从`.bash_profile`中获取该文件即可获得`__git_ps1`. (4认同)
  • 你可以在这里获得`__git_ps1`的"完整"定义.https://github.com/git/git/blob/master/contrib/completion/git-completion.bash它有点沉重,但为您提供有关变基,合并等的好消息. (3认同)

suc*_*uch 14

如果命令__git_ps1失败,您提供的脚本无法检测git repos .将此添加到~/.bash_profile:

source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh
Run Code Online (Sandbox Code Playgroud)

假设您将脚本文件存储为~/.ps1,还添加:

source ~/.ps1
Run Code Online (Sandbox Code Playgroud)
  • 此解决方案还可以为git启用选项卡.
  • 混帐的Mac OS X的安装 有__git_ps1在内,感谢sschuberth和cheapener用于提混帐completion.bash.

  • 我的文件位于/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash.Mountain Lion安装了Xcode CLI. (2认同)

Mat*_*ley 11

在使用内置git的新Yosemite mac上,我使用了这个:

source /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export PS1='\[\e]0;\u@\h: \w\a\]\[\e[32;1m\]\u@\h:\w \[\e[33;1m\]$(__git_ps1 "[%s] ")\[\e[32;1m\]\$ \[\e[0m\]'
Run Code Online (Sandbox Code Playgroud)

注意:在El Capitan上,我不得不改变git脚本的路径,/Applications/Xcode.app/Contents/Developer/usr/share/git-core我想你必须安装XCode才能工作.