簡體   English   中英

PS1 env變量在mac上不起作用

[英]PS1 env variable does not work on mac

我有一個腳本(不是我自己編寫的),它在我的命令提示符下顯示了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

Git的Mac OS X安裝沒有包含__git_ps1

使用:

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

作為替代。

如果命令__git_ps1失敗,則您提供的腳本無法檢測git repos。 將其添加到~/.bash_profile

source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh

假設您將腳本文件存儲為~/.ps1 ,還要添加:

source ~/.ps1

  • 此解決方案還可以為git啟用選項卡。
  • have __git_ps1 included, thanks sschuberth and cheapener for mentioning git-completion.bash. 混帳的Mac OS X的安裝 有__git_ps1在內,感謝sschuberth和cheapener用於提混帳completion.bash。

在使用內置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\]'

注意:在El Capitan上,我不得不將git腳本的路徑更改為/Applications/Xcode.app/Contents/Developer/usr/share/git-core ,我想你必須安裝XCode才能使用。

如果你通過macports(git-core)安裝了git,你應該將以下內容添加到~/.bash_profile

source /opt/local/etc/profile.d/bash_completion.sh
source /opt/local/share/git-core/git-prompt.sh  

git-prompt.sh的位置似乎已經改變了幾次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM