Skip to content

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

Notifications You must be signed in to change notification settings

zenbot/virtualfish

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

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
     # optional, but recommended, plugins
     . path/to/global_requirements.fish
    

    Note: If your config.fish modifies your $PATH, you should ensure that you source virtualfish after those modifications.

  2. Customize your fish_prompt

Usage

Commands

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

Additionally, plugins provide the following commands:

  • global_requirements.fish
    • vf requirements - Edit the global requirements file in your $EDITOR. Applies the requirements to all virtualenvs on exit.

*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, change to the directory, activate the desired virtualenv, and run vf connect.

This will save the name of the virtualenv to the file .venv. If you would prefer to use a different name for this file, you can do so provided you also set VIRTUALFISH_ACTIVATION_FILE in ~/.config/fish/config.fish to the same value. For example, for compatibility with older versions of this script:

set -gx VIRTUALFISH_ACTIVATION_FILE .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>
  • virtualenv_will_create
  • virtualenv_did_create
  • virtualenv_did_create:<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_ACTIVATION_FILE (default: .venv) - the name of the file virtualfish will use for the auto-activation feature.
  • 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.

If you have pip 1.4+ and have used vf requirements to add global requirements that should be installed in all your virtual environments, adding the following configuration variables to ~/.config/fish/config.fish will significantly speed up the installation process:

set -x PIP_USE_WHEEL "true"
set -x PIP_WHEEL_DIR "$HOME/.pip/wheels"
set -x PIP_FIND_LINKS "$HOME/.pip/wheels"
set -x PIP_DOWNLOAD_CACHE "$HOME/.pip/cache"

These are standard pip settings and aren't directly related to virtualfish. The wheels and cache paths can be set to any arbitrary directories you prefer.

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published