Skip to content
forked from cjrh/vim-conda

Change conda environments in the Vim editor (with Jedi-vim support)

License

Notifications You must be signed in to change notification settings

russtoku/vim-conda

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vim-conda

This is a Vim plugin to support Python development using the Conda environment manager.

Install

Vundle is the recommended way. Add this to the section in your vimrc file where all your plugin statements appear:

Plugin 'cjrh/vim-conda'

Super-short summary

When developing Python with Vim, there are two Pythons of interest:

  1. The one that executes your code in a shell command, i.e. :!python %
  2. The (embedded in Vim) one that jedi-vim uses to provide code completion.

Conda is concerned with the first one, i.e. the "shell Python". The second one depends on how you have Vim set up with respect to its own Python scripting support.

This plugin provides a command, CondaChangeEnv, that will

  1. Change the $PATH and $CONDA_DEFAULT_ENV environment variables inside the Vim process, so that new launched processes will have the same environment as if they were launched from a Conda env.
  2. Change the embedded Python sys.path inside Vim so that tools like jedi-vim will provide code completion for the selected env.

Demo

gif screencast of plugin demo

Introduction

The Vim editor can be used to develop Python code. One popular workflow is to edit the text of a code module (e.g. a .py file), and then execute the code with a shell command, such as

:!python %

(where % will be expanded to the name of the current file). Which python will run? Why, the one in the system path of course! But what happens if there is more than one Python executable in the system path? The first one to be found will be the one that runs.

This forms the basis of how virtual environments work. The Conda tool is an environment manager for Python; it also supports package management as part of its feature set, but we are not concerned with that here. Conda allows the user to create multiple, separate Python installations, and switch between them on the command line. It does this by modifying the $PATH (or PATH on Windows) environment variable.

vim-conda makes it easy to perform switching environments right from inside Vim. Now you never have to leave, so the >300 upvoted question on StackOverflow on "how to quit" need no longer concern you ;)

This plugin provides only one single command, CondaChangeEnv, which you can map to an unused key. You can call the command like so:

:CondaChangeEnv<ENTER>

You can map it to a key (e.g. in your vimrc file) like so:

map <F4> :CondaChangeEnv<CR>

When the command is executed, a wildmenu will appear showing the currently available Conda environments on your system. By selection one, the changes to $PATH and the embedded-python sys.path are made. (Unfortunately, the key for triggering wildmode and moving through the wildmenu is hard-coded to <Tab>; I still haven't learned enough vimscript to figure out how to use a user setting.)

In the list of environments, you will also see root as an option if you had previously changed to a non-root env. Selecting root is the same as doing a deactivate in the sense that all the changes made previously are rolled back.

Likewise, when you change from one environment to another, the change is clean in the sense that changes from the first env are reset, before changes for the new env are made. Exactly as would happen on the command line.

Details

The CondaChangeEnv command will trigger wildmode allowing you to tab through the existing Conda environments on your system. When an environment is selected, the following happens:

  • A new environment variable called $CONDA_DEFAULT_ENV is created inside the running Vim process
  • The $PATH variable is set to be the selected Conda env, plus the associated /Scripts folder, as per the usual way the activate script supplied by Conda would modify the path. Note that the $PATH environment variable inside the running Vim process is modified.
  • The sys.path list of the embedded Python instance inside the running Vim process is modified to include the entries for the selected Conda env. This is done so that the Jedi-Vim package will automatically be able to perform code completion within the selected env.

Notes

While testing, I found that I needed the following settings in my vimrc in order to suppress some errors related to other packages:

let g:jedi#force_py_version = 2
let g:UltisnipsUsePythonVersion = 2

More testing is needed to make sure that all the configurations work.

About

Change conda environments in the Vim editor (with Jedi-vim support)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Vim Script 100.0%