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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I think the following is the simplest solution:
Add to
~/.virtualenvs/postactivate
the following:Taken from: http://wiki.hackzine.org/development/python/virtualenv.html
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:
Which yields a command prompt along the lines of:
Switching into a new virtual environment with "workon < name_of_env >" turned the command prompt to something like:
Which was more cluttered than I wanted and the wrong color to boot. I was hoping for something like:
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.
I simply edited the file $WORKON_HOME/postactivate to include these lines:
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).
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.See this snippet that I've used:
I adopted @ivanalejandro0's solution by slimming down the function a bit:
Or if you're feeling really hacky: