【问题标题】:Bash command prompt with virtualenv and git branch带有 virtualenv 和 git 分支的 Bash 命令提示符
【发布时间】:2014-04-30 22:31:17
【问题描述】:

如果我在 git repo 中,我之前的 bash 命令提示符会显示我当前工作目录的最后两个目录和我的 git 分支。我开始在虚拟环境中添加一些项目,当我处理这些项目时,我的命令提示符设置停止工作。

我已经设法让 virtualenv 和我的当前目录显示出来,但我仍然无法让我的 git 分支工作。

目前我得到这个: [name_of_virtualenv if in one] ~/current/directory () $

我怎样才能让我的命令提示符像这样显示 [name_of_virtualenv if in one] ~/current/directory [master] $

.bashrc 文件:

# external settings
# for path in /etc/bashrc /etc/bash.bashrc /etc/bash/bashrc; do
#     [[ -f "$path" ]] && . "$path"
# done

# load rvm function
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

# source brew
if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi

# history storage settings
export HISTCONTROL=ignoredups
export HISTSIZE=1000000
export HISTFILESIZE=1000000
export EDITOR='vim'

SEND_256_COLORS_TO_REMOTE=1

# bash_prompt
# The various escape codes that we can use to color our prompt.
        RED="\[\033[0;31m\]"
     YELLOW="\[\033[1;33m\]"
      GREEN="\[\033[0;32m\]"
       BLUE="\[\033[1;34m\]"
  LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
      WHITE="\[\033[1;37m\]"
 LIGHT_GRAY="\[\033[0;37m\]"
 COLOR_NONE="\[\e[0m\]"

 # Detect whether the current directory is a git repository.
 function is_git_repository {
   git branch > /dev/null 2>&1
 }

 # Determine the branch/state information for this git repository.
 function set_git_branch {
   # Capture the output of the "git status" command.
   git_status="$(git status 2> /dev/null)"

   # Set color based on clean/staged/dirty.
   if [[ ${git_status} =~ "working directory clean" ]]; then
     state="${GREEN}"
   elif [[ ${git_status} =~ "Changes to be committed" ]]; then
     state="${YELLOW}"
   else
     state="${LIGHT_RED}"
   fi

   # Set arrow icon based on status against remote.
   remote_pattern="# Your branch is (.*) of"
   if [[ ${git_status} =~ ${remote_pattern} ]]; then
     if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
       remote="↑"
     else
       remote="↓"
     fi
   else
     remote=""
   fi
   diverge_pattern="# Your branch and (.*) have diverged"
   if [[ ${git_status} =~ ${diverge_pattern} ]]; then
     remote="↕"
   fi

   # Get the name of the branch.
   branch_pattern="^# On branch ([^${IFS}]*)"
   if [[ ${git_status} =~ ${branch_pattern} ]]; then
     branch=${BASH_REMATCH[1]}
   fi

   # Set the final branch string.
   BRANCH="${state}(${branch})${remote}${COLOR_NONE} "
 }

 # Return the prompt symbol to use, colorized based on the return value of the
 # previous command.
 function set_prompt_symbol () {
   if test $1 -eq 0 ; then
       PROMPT_SYMBOL="\$"
   else
       PROMPT_SYMBOL="${LIGHT_RED}\$${COLOR_NONE}"
   fi
 }

 # Determine active Python virtualenv details.
 function set_virtualenv () {
   if test -z "$VIRTUAL_ENV" ; then
       PYTHON_VIRTUALENV=""
   else
       PYTHON_VIRTUALENV="${BLUE}[`basename \"$VIRTUAL_ENV\"`]${COLOR_NONE} "
   fi
 }

 # Set the full bash prompt.
 function set_bash_prompt () {
   # Set the PROMPT_SYMBOL variable. We do this first so we don't lose the
   # return value of the last command.
   set_prompt_symbol $?

   # Set the PYTHON_VIRTUALENV variable.
   set_virtualenv

   # Set the BRANCH variable.
   if is_git_repository ; then
     set_git_branch
   else
     BRANCH=''
   fi

   # Set the bash prompt variable.
   PS1="${PYTHON_VIRTUALENV}${YELLOW}\w${COLOR_NONE} ${BRANCH}${PROMPT_SYMBOL} "
 }

 # Tell bash to execute this function just before displaying its prompt.
 PROMPT_COMMAND=set_bash_prompt

pwdtail () { #returns the last 2 fields of the working directory
    pwd|awk -F/ '{nlast = NF -1;print $nlast"/"$NF}'
}

export PATH=/usr/local/bin:/usr/local/share/npm/bin:~/bin:$PATH
export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
export EDITOR='vim'

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

export WORKON_HOME=$HOME/.envs
source /usr/local/bin/virtualenvwrapper.sh

【问题讨论】:

    标签: git bash virtualenv command-prompt


    【解决方案1】:

    通过在我的 bashrc 文件中将命令提示符代码更改为以下内容,我得到了它的工作方式:

    # bash_prompt
    # The various escape codes that we can use to color our prompt.
            RED="\[\033[0;31m\]"
         YELLOW="\[\033[1;33m\]"
          GREEN="\[\033[0;32m\]"
           BLUE="\[\033[1;34m\]"
      LIGHT_RED="\[\033[1;31m\]"
    LIGHT_GREEN="\[\033[1;32m\]"
          WHITE="\[\033[1;37m\]"
     LIGHT_GRAY="\[\033[0;37m\]"
     COLOR_NONE="\[\e[0m\]"
    
     # Detect whether the current directory is a git repository.
     function is_git_repository {
       git branch > /dev/null 2>&1
     }
    
     function set_git_branch {
       # Set the final branch string
       BRANCH=`parse_git_branch`
       local TIME=`fmt_time` # format time for prompt string
     }
    
     function parse_git_branch() {
       git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
     }
    
     function parse_git_dirty() {
       [[ $(git status 2> /dev/null | tail -n1) != *"working directory clean"* ]] && echo "*"
     }
    
     fmt_time () { #format time just the way I likes it
         if [ `date +%p` = "PM" ]; then
             meridiem="pm"
         else
             meridiem="am"
         fi
         date +"%l:%M:%S$meridiem"|sed 's/ //g'
     }
    
     # Return the prompt symbol to use, colorized based on the return value of the
     # previous command.
     function set_prompt_symbol () {
       if test $1 -eq 0 ; then
           PROMPT_SYMBOL="\$"
       else
           PROMPT_SYMBOL="${LIGHT_RED}\$${COLOR_NONE}"
       fi
     }
    
     # Determine active Python virtualenv details.
     function set_virtualenv () {
       if test -z "$VIRTUAL_ENV" ; then
           PYTHON_VIRTUALENV=""
       else
           PYTHON_VIRTUALENV="${BLUE}[`basename \"$VIRTUAL_ENV\"`]${COLOR_NONE} "
       fi
     }
    
     # Set the full bash prompt.
     function set_bash_prompt () {
       # Set the PROMPT_SYMBOL variable. We do this first so we don't lose the
       # return value of the last command.
       set_prompt_symbol $?
    
       # Set the PYTHON_VIRTUALENV variable.
       set_virtualenv
    
       # Set the BRANCH variable.
       if is_git_repository ; then
         set_git_branch
       else
         BRANCH=''
       fi
    
       # Set the bash prompt variable.
       PS1="${PYTHON_VIRTUALENV}${YELLOW}\w${COLOR_NONE} ${BRANCH}${PROMPT_SYMBOL} "
     }
    
     # Tell bash to execute this function just before displaying its prompt.
     PROMPT_COMMAND=set_bash_prompt
    

    【讨论】:

    • 谢谢,您的回答帮助我按照自己的方式设置提示。
    • 我做了一些小改动以避免一些 bash 错误。我将 fmt_time 第一行更改为:datevar=´date +%p´;如果 [ -n $datevar ] && [ "$datevar" = "PM" ];那么
    【解决方案2】:

    为简单起见,要显示 git 提示符和虚拟环境,您只需要这样:

    export PS1="\[\033[1;31m\]`basename \"$VIRTUAL_ENV\"` $(__git_ps1 "(git:%s)")"
    

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 2015-06-18
      • 2017-07-31
      • 1970-01-01
      • 2017-08-23
      • 2010-10-23
      • 2017-05-25
      • 2013-12-05
      • 2020-07-06
      相关资源
      最近更新 更多