如何更改默认的virtualenv提示符? [英] How do I change the default virtualenv prompt?

查看:103
本文介绍了如何更改默认的virtualenv提示符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改默认的Virtualenvwrapper提示符?默认情况下,使用"workon< _name_of_env_>"之类的命令在特定的虚拟环境上工作,会将virtualenv的名称添加到提示中.如果您未使用默认命令提示符,则此方法可能效果不佳.

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.

推荐答案

默认情况下,当您使用命令"workon< name_of_env>"切换到virtualenv时,virtualenvwrapper会在字符串(< name_of_env >)转到命令提示符.问题是我在Bash提示符下设置了以下行:

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:

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

哪个会产生以下命令提示符:

Which yields a command prompt along the lines of:

< old_line >

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

使用"workon< name_of_env>"切换到新的虚拟环境会将命令提示符更改为:

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

< 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之前曾指出 virtualenvwrapper的钩子是解决方案,但我认为我会发布实际代码,以节省一分钟的时间.

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

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

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

# 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\[\033[$PROMPT_COLOR1\](`basename \"$VIRTUAL_ENV\"`) ${_OLD_VIRTUAL_PS1:2:${#_OLD_VIRTUAL_PS1}}"
else
    PS1="\[\033[$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).

这篇关于如何更改默认的virtualenv提示符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆