4 min read

We will talk here how to send your codes throughout VI (Vi IMproved) to IPython a.k.a jupyter.

When you install IPython, it also creates a symlink to Jupyter, which is the “new” name for IPython. It has changed since Jupyter is compatible with many scientific computing languages that are exclusively Python, so now the word IPython is a little bit deprecated instead of jupyter. IPython itself is focused on interactive Python, part of which is providing a Python kernel for Jupyter.

To start my Python codes I have been using VIM for better usability and facility to reproduce my customizations and installing procedures and also using VIM as a remarkable IDE (see here a better description for this customization).

If you don’t know VIM (Vi IMproved), it is a lightweight and fast editor, which can make your programing time much more efficient with its integrated shortcuts and many plugins that can be coupled to VIM, speeding up many functionalities. One of these functionalities was written by Paul Ivanov, named vim-ipython which makes possible a conversation between IPython and VIM.


Installing

A simple apt-get install vim will not solve this issue, because vim needs to be compatible with your Python distribution or any other accessory language that you use (e.g. Ruby, Python, etc).

First of all you need to download the vim source code, to compile it against your Python distribution. I have been using python 2.7 for scientific programing, but it is also possible to compile vim for other Python versions (e.g. 2.6 or 3.x).

An important observation is that your vim must be compiled considering your Python version, usually the version chosen when a virtualenv (Virtual Environment) is created.

Most of the time different projects require different dependencies, so isolating environments is the best way to keep your executables apart from system wide. This tutorial instructs you how to use virtualenv.

Inside your virtualenv, you should install IPython as follows:

$ pip install pyside
$ pip install "ipython[all]"

It is important to install pyside to make possible execute ipython qtconsole instead of simple ipython (or jupyter).

After installing IPython inside your virtualenv, you can start the vim compilation.

To avoid conflicts between dependencies I use the method that removes all your previous vim installations.

sudo apt-get remove vim-common vim-runtime

Install the dependencies required to compile vim:

sudo apt-get build-dep vim

Download vim source code from Github repository:

git clone https://github.com/vim/vim.git

And now just compile vim including your preferences. If you want more customizations you can also uncomment lines in the Makefile at vim/src before starting the steps below.

cd vim/src
./configure --enable-pythoninterp --with-features=huge --prefix=$HOME/opt/vim
make && make install
echo 'export PATH=$HOME/opt/vim/bin:PATH' >> $HOME/.bashrc

The vim-ipython plugin

To install the vim-ipython plugin you need to download the plugin and copy it to ~/.vim/ftplugin/python to load automatically.

There are simpler ways to install and manage vim plugins, using vundle. With this system you just include the name of github repository inside your .vimrc file and it will be possible to install other plugins. You can read more about the vundle usage and the installation here.

Interacting with vim and IPython

After opening a terminal, you need to be working inside your virtualenv to let IPython recognize the plugins you have previously installed inside your environment.

ipython qtconsole

If you prefer to use IPython notebooks:

ipython notebook

Both initializations will let vim to catch the running IPython kernel and be able to interact with. An interesting note about the second approach (e.g. running ipython notebook) is that you must open an existing ipython notebook file (.ipynb) or start a new one at the new icon at the upper right corner.

If you use ipython qtconsole it will display a single window outside your terminal.

The second step is opening a .py file with your vim editor from a second tab of your terminal.

When or .py file is opened, you can execute the vim command:

:IPython

And the vim-ipython plugin will recognize the existing IPython kernel.

The next step is to send lines of your codes to IPython or entire blocks of code, selecting it with visual mode throughout . To execute the entire file just use the key (this is similar to use the magic %run inside IPython).

After sending lines to IPython, your ipython qtconsole will remain silent, and your vim window will split vertically showing the lines executed.

You can also get back object introspection and word completions in ipython qtconsole, like what you get with: object? and object. in IPython.

When you insert new variables at ipython notebook it will be displayed inside your vim spilted window and will show a message: “vim-ipython shell updated on (idle)”.

More specific usage of this amazing tool can be read at vim-ipython github repository including customizations.

Have a nice coding time coupling your vim with IPython.

Now that your vim and IPython are successfully coupled strike while the iron is hot and discover how to do more with Jupyter Notebook in this brilliant article.

About the author

Arnaldo D’Amaral Pereira Granja Russo is PhD in Biological Oceanography and researcher at Instituto Ambiental Boto Flipper. While not teaching or programming he is surfing, climbing or paragliding. You can find him at www.ciclotux.org or at his Github page.


Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

LEAVE A REPLY

Please enter your comment!
Please enter your name here