bash 如何更改默认的 virtualenv 提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10406926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:10:15  来源:igfitidea点击:

How do I change the default virtualenv prompt?

pythonbashvirtualenvcommand-promptvirtualenvwrapper

提问by kevin

How do you change the default Virtualenvwrapper prompt? By default, working on a particular virtual environment with a command like "workon <_name_of_env_>" prepends the name of the virtualenv to your prompt. This may work poorly if you're not using a default command prompt.

如何更改默认的 Virtualenvwrapper 提示?默认情况下,使用“workon <_name_of_env_>”之类的命令在特定虚拟环境中工作会在提示前添加 virtualenv 的名称。如果您没有使用默认命令提示符,这可能效果不佳。

采纳答案by kevin

By default, when you switch into a virtualenv with the command "workon < name_of_env >", virtualenvwrapper prepends a string along the lines of "(< name_of_env >) " to your command prompt. The problem is that I set my Bash prompt with the lines:

默认情况下,当您使用命令“workon < name_of_env >”切换到 virtualenv 时,virtualenvwrapper 会在命令提示符前添加一个“(< name_of_env >)”行的字符串。问题是我用以下行设置了我的 Bash 提示:

PROMPT_COLOR1='0;36m'
PROMPT_COLOR2='1;34m'
PS1='\n\[3[$PROMPT_COLOR1\](\t)\[3[$PROMPT_COLOR2\] \u @ \w \n\[3[$PROMPT_COLOR1\]$ \[3[0;39m\]'

Which yields a command prompt along the lines of:

这会产生一个命令提示符,如下所示:

< old_line >

(19:11:05) kevin @ ~/research 
$ 

Switching into a new virtual environment with "workon < name_of_env >" turned the command prompt to something like:

使用“workon < name_of_env >”切换到一个新的虚拟环境,命令提示符变成了这样的:

< old_line >
(< name_of_env >)
(19:11:05) kevin @ ~/research 
$ 

Which was more cluttered than I wanted and the wrong color to boot. I was hoping for something like:

这比我想要的更混乱,而且启动的颜色错误。我希望是这样的:

< old_line >

(< name_of_env >) (19:11:05) kevin @ ~/research 
$ 

Ian Bicking has previously pointed out that virtualenvwrapper's hooks were the solutionbut I figured I'd post my actual code to maybe save someone else a minute down the line.

Ian Bicking 之前曾指出virtualenvwrapper 的钩子是解决方案,但我想我会发布我的实际代码,以便为其他人节省一分钟。

I simply edited the file $WORKON_HOME/postactivate to include these lines:

我只是编辑了 $WORKON_HOME/postactivate 文件以包含以下几行:

# color virtualenv name properly and put it after the \n if there is one at the start of the prompt
if [ ${_OLD_VIRTUAL_PS1:0:2} == '\n' ]; then
    PS1="\n\[3[$PROMPT_COLOR1\](`basename \"$VIRTUAL_ENV\"`) ${_OLD_VIRTUAL_PS1:2:${#_OLD_VIRTUAL_PS1}}"
else
    PS1="\[3[$PROMPT_COLOR1\](`basename \"$VIRTUAL_ENV\"`) $_OLD_VIRTUAL_PS1 "
fi

and voila! The color and location are correct and it even works when you switch directly from one virtual environment to another (which I hadn't expected).

瞧!颜色和位置是正确的,当您直接从一个虚拟环境切换到另一个虚拟环境时,它甚至可以工作(这是我没想到的)。

回答by ivanalejandro0

If you are working on a custom PS1 (as I when found out this issue), I recommend you to disable prompt change, use export VIRTUAL_ENV_DISABLE_PROMPT=1(see virtualenv docs), and make your own virtualenv prompt in order to add to your PS1.

如果您正在处理自定义 PS1(就像我发现此问题时一样),我建议您禁用提示更改,使用export VIRTUAL_ENV_DISABLE_PROMPT=1(请参阅virtualenv 文档),并制作您自己的 virtualenv 提示以便添加到您的 PS1。

See this snippet that I've used:

查看我使用过的这个片段:

function virtualenv_info(){
    # Get Virtual Env
    if [[ -n "$VIRTUAL_ENV" ]]; then
        # Strip out the path and just leave the env name
        venv="${VIRTUAL_ENV##*/}"
    else
        # In case you don't have one activated
        venv=''
    fi
    [[ -n "$venv" ]] && echo "(venv:$venv) "
}

# disable the default virtualenv prompt change
export VIRTUAL_ENV_DISABLE_PROMPT=1

VENV="$(virtualenv_info)";
# the '...' are for irrelevant info here.
export PS1="... ${VENV} ..."

回答by Dror

I think the following is the simplest solution:

我认为以下是最简单的解决方案:

Add to ~/.virtualenvs/postactivatethe following:

添加~/.virtualenvs/postactivate以下内容:

PS1="\[\e[1;33;45m\] (`basename \"$VIRTUAL_ENV\"`) \[\e[0m\]$_OLD_VIRTUAL_PS1"

Taken from: http://wiki.hackzine.org/development/python/virtualenv.html

取自:http: //wiki.hackzine.org/development/python/virtualenv.html

回答by dtk

I adopted @ivanalejandro0's solutionby slimming down the function a bit:

我采用了@ivanalejandro0 的解决方案稍微精简了功能:

function virtualenv_info {
    # Get Virtual Env
    if [[ -n "$VIRTUAL_ENV" ]]; then
        # Strip out the path and just leave the env name
        echo "(venv:${VIRTUAL_ENV##*/})"
    fi

Or if you're feeling really hacky:

或者,如果你真的觉得自己很笨拙:

function virtualenv_info {
    [[ -n "$VIRTUAL_ENV" ]] && echo "(venv:${VIRTUAL_ENV##*/})"
}

回答by TomB

One could reduce the function in @ivanalejandro0's solutionby using an "alternate value" parameter expansion. Also, as @crimson-egret commented, the call can be right in PS1 without the VENV intermediate:

可以通过使用“替代值”参数扩展来减少@ivanalejandro0 解决方案中的函数。此外,正如@crimson-egret 评论的那样,在没有 VENV 中间体的情况下,该调用可以在 PS1 中进行:

function __virtualenv_ps1 {
    echo "${VIRTUAL_ENV:+(venv:${VIRTUAL_ENV##*/})}"
}

# disable the default virtualenv prompt change
export VIRTUAL_ENV_DISABLE_PROMPT=1

# the '...' are for irrelevant info here.
export PS1="... $(__virtualenv_ps1) ..."