├── .gitignore ├── LICENSE ├── README.mkd ├── autoload ├── pyvenv.py └── virtualenv.vim ├── doc └── virtualenv.txt └── plugin └── virtualenv.vim /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | /doc/tags 3 | 4 | ### Template taken from github's gitignore repository 5 | ### Python template 6 | # Byte-compiled / optimized / DLL files 7 | __pycache__/ 8 | *.py[cod] 9 | *$py.class 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /README.mkd: -------------------------------------------------------------------------------- 1 | virtualenv.vim 2 | ============== 3 | 4 | When using :python or :!python, it will only have access to the environment outside of any virtualenvs. If you're working with packages that are only installed in a virtualenv, they will not be available to Vim. 5 | 6 | *__Until now!__* The virtualenv.vim plugin will modify python's sys.path and the $PATH environment variable so that anything done with :python or :!python will behave like you would expect for the chosen virtualenv. 7 | 8 | If compiled with python support, Vim will have a :python command, but this will be tied to whatever version is the default on your system. If this is the version of python that you use, or you're using a Linux distribution with a sensible package manager (like Debian or Ubuntu), you likely will not have to do anything more than install the plugin. If not, then you will likely have to recompile vim with your version of python. 9 | 10 | Usage examples 11 | ============== 12 | 13 | Deactivate the current virtualenv 14 | 15 | :VirtualEnvDeactivate 16 | 17 | List all virtualenvs 18 | 19 | :VirtualEnvList 20 | 21 | Activate the 'spam' virtualenv 22 | 23 | :VirtualEnvActivate spam 24 | 25 | If you're unsure which one to activate, you could always use tab completion 26 | 27 | :VirtualEnvActivate 28 | 29 | If the shell that started vim had $VIRTUAL\_ENV set, omitting the name will 30 | imply usage of this value. 31 | 32 | If you're a virtualenvwrapper user and have $PROJECT\_HOME set, omitting the 33 | name will try to guess which virtualenv to activate based on the current 34 | filename. 35 | 36 | You can even show the current virtualenv in the statusline with the included function. 37 | 38 | Or, for those with a properly configured Powerline (the virtualenv segment is not enabled by default), your virtualenv indicator will toggle on or off accordingly. 39 | 40 | For more detailed help 41 | 42 | :help virtualenv 43 | 44 | -------------------------------------------------------------------------------- /autoload/pyvenv.py: -------------------------------------------------------------------------------- 1 | import vim, os, sys 2 | 3 | prev_syspath = None 4 | 5 | activate_content = """ 6 | try: 7 | __file__ 8 | except NameError: 9 | raise AssertionError( 10 | "You must run this like execfile('path/to/activate_this.py', dict(__file__='path/to/activate_this.py'))") 11 | import sys 12 | import os 13 | 14 | old_os_path = os.environ['PATH'] 15 | os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path 16 | base = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | if sys.platform == 'win32': 18 | site_packages = os.path.join(base, 'Lib', 'site-packages') 19 | else: 20 | version = '%s.%s' % (sys.version_info.major, sys.version_info.minor) 21 | site_packages = os.path.join(base, 'lib', 'python%s' % version, 'site-packages') 22 | prev_sys_path = list(sys.path) 23 | import site 24 | site.addsitedir(site_packages) 25 | sys.real_prefix = sys.prefix 26 | sys.prefix = base 27 | # Move the added items to the front of the path: 28 | new_sys_path = [] 29 | for item in list(sys.path): 30 | if item not in prev_sys_path: 31 | new_sys_path.append(item) 32 | sys.path.remove(item) 33 | sys.path[:0] = new_sys_path 34 | """ 35 | 36 | def activate(env): 37 | global prev_syspath 38 | prev_syspath = list(sys.path) 39 | activate = os.path.join(env, (sys.platform == 'win32') and 'Scripts' or 'bin', 'activate_this.py') 40 | try: 41 | fo = open(activate) 42 | f = fo.read() 43 | fo.close() 44 | except: 45 | f = activate_content 46 | 47 | code = compile(f, activate, 'exec') 48 | exec(code, dict(__file__=activate)) 49 | 50 | 51 | def deactivate(): 52 | global prev_syspath 53 | try: 54 | sys.path[:] = prev_syspath 55 | prev_syspath = None 56 | except: 57 | pass 58 | -------------------------------------------------------------------------------- /autoload/virtualenv.vim: -------------------------------------------------------------------------------- 1 | if has('python3') 2 | python3 import sys, vim 3 | python3 if vim.eval('expand(":p:h")') not in sys.path: sys.path.append(vim.eval('expand(":p:h")')) 4 | python3 import pyvenv 5 | endif 6 | if has('python') 7 | python import sys, vim 8 | python if vim.eval('expand(":p:h")') not in sys.path: sys.path.append(vim.eval('expand(":p:h")')) 9 | python import pyvenv 10 | endif 11 | 12 | function! virtualenv#activate(...) 13 | let name = a:0 > 0 ? a:1 : '' 14 | let silent = a:0 > 1 ? a:2 : 0 15 | let env_dir = '' 16 | if len(name) == 0 "Figure out the name based on current file 17 | if isdirectory($VIRTUAL_ENV) 18 | let name = fnamemodify($VIRTUAL_ENV, ':t') 19 | let env_dir = $VIRTUAL_ENV 20 | elseif isdirectory($PROJECT_HOME) 21 | let fn = expand('%:p') 22 | let pat = '^'.$PROJECT_HOME.'/' 23 | if fn =~ pat 24 | let name = fnamemodify(substitute(fn, pat, '', ''), ':h') 25 | if name != '.' "No project directory 26 | let env_dir = g:virtualenv_directory.'/'.name 27 | endif 28 | endif 29 | endif 30 | else 31 | let env_dir = g:virtualenv_directory.'/'.name 32 | endif 33 | 34 | "Couldn't figure it out, so DIE 35 | if !isdirectory(env_dir) 36 | if !silent 37 | echoerr "No virtualenv could be auto-detected and activated." 38 | endif 39 | return 40 | endif 41 | 42 | let bin = env_dir.(has('win32')? '/Scripts': '/bin') 43 | call virtualenv#deactivate() 44 | 45 | let s:prev_path = $PATH 46 | 47 | " Prepend bin to PATH, but only if it's not there already 48 | " (activate_this does this also, https://github.com/pypa/virtualenv/issues/14) 49 | let PATHsep = has('win32') ? ';' : ':' 50 | 51 | if $PATH[:len(bin)] != bin.PATHsep 52 | let $PATH = bin.PATHsep.$PATH 53 | endif 54 | 55 | if has('python') 56 | python pyvenv.activate(vim.eval('l:env_dir')) 57 | endif 58 | if has('python3') 59 | python3 pyvenv.activate(vim.eval('l:env_dir')) 60 | endif 61 | 62 | let g:virtualenv_name = name 63 | let $VIRTUAL_ENV = env_dir 64 | 65 | if exists("*airline#extensions#virtualenv#update") 66 | call airline#extensions#virtualenv#update() 67 | endif 68 | endfunction 69 | 70 | function! virtualenv#deactivate() 71 | if has('python') 72 | python pyvenv.deactivate() 73 | endif 74 | if has('python3') 75 | python3 pyvenv.deactivate() 76 | endif 77 | 78 | unlet! g:virtualenv_name 79 | 80 | let $VIRTUAL_ENV = '' " can't delete parent variables 81 | 82 | if exists('s:prev_path') 83 | let $PATH = s:prev_path 84 | endif 85 | 86 | if exists("*airline#extensions#virtualenv#update") 87 | call airline#extensions#virtualenv#update() 88 | endif 89 | endfunction 90 | 91 | function! virtualenv#list() 92 | for name in virtualenv#names('') 93 | echo name 94 | endfor 95 | endfunction 96 | 97 | function! virtualenv#statusline() 98 | if exists('g:virtualenv_name') 99 | return substitute(g:virtualenv_stl_format, '\C%n', g:virtualenv_name, 'g') 100 | else 101 | return '' 102 | endif 103 | endfunction 104 | 105 | function! virtualenv#names(prefix) 106 | let venvs = [] 107 | for dir in split(glob(g:virtualenv_directory.'/'.a:prefix.'*'), '\n') 108 | if !isdirectory(dir) 109 | continue 110 | endif 111 | let fn = dir.(has('win32')? '/Scripts': '/bin').'/activate' 112 | if !filereadable(fn) 113 | continue 114 | endif 115 | call add(venvs, fnamemodify(dir, ':t')) 116 | endfor 117 | return venvs 118 | endfunction 119 | -------------------------------------------------------------------------------- /doc/virtualenv.txt: -------------------------------------------------------------------------------- 1 | *virtualenv.txt* Activate a python virtualenv within Vim 2 | 3 | Author: Jeremy Cantrell *virtualenv-author* 4 | License: Same terms as Vim itself (see |license|) 5 | 6 | INTRODUCTION *virtualenv* *virtualenv.vim* 7 | 8 | The virtualenv plugin allows you to activate and deactivate a virtualenv 9 | within a live Vim session. 10 | 11 | COMMANDS *virtualenv-commands* 12 | 13 | :VirtualEnvList *:VirtualEnvList* 14 | List all available virtualenvs. 15 | 16 | :VirtualEnvDeactivate *:VirtualEnvDeactivate* 17 | Deactivate the current virtualenv. 18 | 19 | :VirtualEnvActivate [name] *:VirtualEnvActivate* 20 | Activate a virtualenv. The name of the virtualenv can be completed with 21 | at the command line. 22 | 23 | If name is not specified, and you have $PROJECT_HOME set, the name will be 24 | guessed based on the current filename. 25 | 26 | CONFIGURATION *virtualenv-configuration* 27 | 28 | g:virtualenv_loaded *g:virtualenv_loaded* 29 | If set in your |vimrc|, virtualenv.vim is not loaded. 30 | 31 | g:virtualenv_directory *g:virtualenv_directory* 32 | The directory that contains the virtualenvs. If you're a virtualenvwrapper 33 | user and you have $WORKON_HOME set, it will default to this. Otherwise it 34 | will default to ~/.virtualenvs. 35 | 36 | g:virtualenv_auto_activate *g:virtualenv_auto_activate* 37 | If set, an attempt will be made to detect any active virtualenv, and 38 | activate it. 39 | 40 | Example: > 41 | let g:virtualenv_directory = '/path/to/virtualenvs' 42 | < 43 | g:virtualenv_stl_format *g:virtualenv_stl_format* 44 | Format string for the statusline function. 45 | 46 | Example: > 47 | let g:virtualenv_stl_format = '[%n]' 48 | < 49 | To use the statusline flag, this must appear in your |'statusline'| setting: > 50 | %{virtualenv#statusline()} 51 | < 52 | The content is derived from the |g:virtualenv_stl_format| variable. 53 | 54 | Powerline users can see their statuslines too. After configuring powerline 55 | according to its documentation to support a virtualenv segment, Powerline will 56 | read the value of $VIRTUAL_ENV and display it. 57 | 58 | vim:tw=78:et:ft=help:norl: 59 | -------------------------------------------------------------------------------- /plugin/virtualenv.vim: -------------------------------------------------------------------------------- 1 | if exists("g:virtualenv_loaded") 2 | finish 3 | endif 4 | 5 | let g:virtualenv_loaded = 1 6 | 7 | let s:save_cpo = &cpo 8 | set cpo&vim 9 | 10 | if !has('python3') && !has('python') 11 | finish 12 | endif 13 | 14 | if !exists("g:virtualenv_auto_activate") 15 | let g:virtualenv_auto_activate = 1 16 | endif 17 | 18 | if !exists("g:virtualenv_stl_format") 19 | let g:virtualenv_stl_format = '%n' 20 | endif 21 | 22 | if !exists("g:virtualenv_directory") 23 | if isdirectory($WORKON_HOME) 24 | let g:virtualenv_directory = $WORKON_HOME 25 | else 26 | let g:virtualenv_directory = '~/.virtualenvs' 27 | endif 28 | endif 29 | 30 | let g:virtualenv_directory = expand(g:virtualenv_directory) 31 | 32 | command! -bar VirtualEnvList :call virtualenv#list() 33 | command! -bar VirtualEnvDeactivate :call virtualenv#deactivate() 34 | command! -bar -nargs=? -complete=customlist,s:CompleteVirtualEnv VirtualEnvActivate :call virtualenv#activate() 35 | 36 | function! s:Error(message) 37 | echohl ErrorMsg | echo a:message | echohl None 38 | endfunction 39 | 40 | function! s:CompleteVirtualEnv(arg_lead, cmd_line, cursor_pos) 41 | return virtualenv#names(a:arg_lead) 42 | endfunction 43 | 44 | " DEPRECATED: Leaving in for compatibility 45 | function! VirtualEnvStatusline() 46 | return virtualenv#statusline() 47 | endfunction 48 | 49 | if g:virtualenv_auto_activate == 1 50 | call virtualenv#activate('', 1) 51 | endif 52 | 53 | let &cpo = s:save_cpo 54 | --------------------------------------------------------------------------------