/virtualfish

A Fish wrapper for Ian Bicking's virtualenv, based loosely on Doug Hellman's virtualenvwrapper for Bash.

virtualfish

A Fish Shell wrapper for Ian Bicking's virtualenv, somewhat loosely based on Doug Hellman's virtualenvwrapper for Bash.

Installation

  1. Source virtual.fish somewhere in your ~/.config/fish/config.fish, like this:

     . path/to/virtual.fish
    
  2. Customize your fish_prompt

Usage

Commands

  • acvirtualenv <envname> (or workon <envname>*) - Activate a virtualenv. (Note: Doesn't use the activate.fish script provided by virtualenv.)
  • devirtualenv (or deactivate*) - Deactivate the current virtualenv.
  • mkvirtualenv [<options>] <envname> - Create a virtualenv. Note that <envname> must be last.
  • rmvirtualenv <envname> - Delete a virtualenv.
  • lsvirtualenv - List the available virtualenvs.
  • cdvirtualenv - Change directory to currently-activated virtualenv.
  • connvirtualenv - Connect the current virtualenv to the current directory, so that it is always active when you are in it.

*with VIRTUALFISH_COMPAT_ALIASES switched on - see Configuration Variables below.

Automatic Activation

virtualfish can automatically activate a virtualenv when you are in a certain directory. To configure it to do so, place a file inside the directory called .vfenv, containing the name of the virtualenv.

$ echo myvirtualenvname > .vfenv

Variables

  • VIRTUAL_ENV - Path to the currently active virtualenv.
    • Tips: use basename to get the virtualenv's name, or set -q to see whether a virtualenv is active at all.
  • VF_AUTO_ACTIVATED - Set if the currently activated virtualenv was automatically activated.

Events

virtualfish emits Fish events instead of using hook scripts. To hook in to events that virtualfish emits, write a function like this:

function myfunc --on-event virtualenv_did_activate
	echo "The virtualenv" (basename $VIRTUAL_ENV) "was activated"
end

You can save your function using funcsave, put it in .config/fish/config.fish, or put it anywhere Fish will see it before it needs to run.

Some events are emitted twice, once normally and once with the name of the virtualenv as part of the event name. This is to make it easier to listen for events relevant to one specific virtualenv, for example:

function myfunc --on-event virtualenv_did_activate:my_site_env
	set -gx DJANGO_SETTINGS_MODULE mysite.settings
end

The full list of events is:

  • virtualenv_will_activate
  • virtualenv_will_activate:<env name>
  • virtualenv_did_activate
  • virtualenv_did_activate:<env name>
  • virtualenv_will_deactivate
  • virtualenv_will_deactivate:<env name>
  • virtualenv_did_deactivate
  • virtualenv_did_deactivate:<env name>

Configuration Variables

All of these must be set before virtual.fish is sourced in your ~/.config/fish/config.fish.

  • VIRTUALFISH_HOME (default: ~/.virtualenvs) - where all your virtualenvs are kept.
  • VIRTUALFISH_COMPAT_ALIASES - set this to create aliases for workon and deactivate a la virtualenvwrapper. Caveat: deactivate exists (and can be overwritten) even when a virtualenv is not active.

Customizing Your fish_prompt

virtualfish doesn't attempt to mess with your prompt. Since Fish's prompt is a function, it is both much less straightforward to change it automatically, and much more convenient to simply customize it manually to your liking.

The easiest way to add virtualenv to your prompt is to type funced fish_prompt, add the following line in somewhere:

if set -q VIRTUAL_ENV
	echo -n -s (set_color -b blue white) "(" (basename "$VIRTUAL_ENV") ")" (set_color normal) " "
end

Then, type funcsave fish_prompt to save your new prompt to disk.

To Do

  • Emit more Fish events where virtualenvwrapper would use hook scripts
  • mktempenv, showvirtualenv, cpvirtualenv
  • long form of lsvirtualenv
  • Project management

License

Copyright (C) 2012 Adam Brenecki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.