Here, I would like to keep a note on how I constructed my vim environment. In particular, I need the following features for my python development.

My need (in the ideal world)

  • Syntax support (e.g., highlighting).
  • Autocompletion - for files and variables.
  • Navigate to other files.
  • Compatible with multiple environment, mostly maintained by conda.
  • Good support of snakmake.
  • Interactive python/R session to test the script.

1. Vim installation

I first updated vim to version 8.2, installing from the source (from the official github repo. Python should be used to compile vim, using the following options for configuration:

./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-python3interp=yes \
            --with-python3-config-dir=$(python3-config --configdir) \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope

Check this thread for dependancy of the installation.

In my case (Linux mint), I need to set LDFLAGS to -fno-lto before configuration. Otherwise gcc gave error. (check this thread)

2. Plugin installation with Vundle

Then, plugins are installed using Vundle, mostly conscerns editing .vimrc. To install Vundle, run the following command (see also the official repository of Vundle).

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

To install a plugin with Vundle, add the following in .vimrc:

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'NAME_OF_THE_PLUGIN'

call vundle#end()            " required

Note that for plugins from a github repository can be referred like: ncm2/ncm2. For the actual installation, execute :PluginInstall once after starting vim.

Key plugins include (final .vimrc is also available below):

  • jedi-vim - python auto-completion support
  • NERDTree - for browing files in vim
  • Nvim-R - for R integration
  • ncm-R - for asynchronous autocompletion when editing R scripts

Note that pynvim and neovim are required for some of the plugins. (install via conda or pip)

3. python sytax support for Snakefile

I obtained the syntax definition of snakemake for vim by Jay Hesselberth (@ University of Colorado), and then merely added the following line to start jedi-vim, which is executed only for python files (.py) by default.

runtime! ftplugin/python/jedi.vim after/ftplugin/python/jedi.vim

4. Support to multiple environments by conda

By activating environments using conda (by conda activate), you can switch R or python version used in vim support. For instance, you will see autocompletion for a python package if the active envrionment have the package installed. Vice versa, if you activate an enviornment that lacks the package, then you will see no autocompletion for this package. Also, if there is an specific version of R installed within an environment, then the very version of R will be used in Nvim-R upon activation of the environment.

There is a catch though. Some plugin may not function properly, if you have different version of python available in the environment. Especially, some plugins require python 3, so they will not work (at least, not properly) when python 2 is there.

Also, you need to install pynvim and neovim (from conda-forge channel) in each environment, using: conda install -c conda-forge neovim pynvim.

The final outcome files:


Further reference:

  • I took the useful vim plugins for Python editing from the following instruction.