项目作者: jupyter-vim

项目描述 :
Make Vim talk to Jupyter kernels
高级语言: Python
项目地址: git://github.com/jupyter-vim/jupyter-vim.git
创建时间: 2019-06-27T13:00:19Z
项目社区:https://github.com/jupyter-vim/jupyter-vim

开源协议:Other

下载


Jupyter-Vim

A two-way integration between Vim and Jupyter. Develop code on a Jupyter notebook without leaving the terminal. Send lines from Vim to a jupyter qtconsole. Have a MATLAB-like “cell-mode”.

Currently supports the following kernels: Bash, C++, C, Java, Javascript, Julia, Perl, Raku, Python, Raku, Ruby, Rust, and R. Find more information here

Video demo

Installation of the plugin

To install this plugin, you should use one of the following methods.

On Windows, replace in the next sections the Unix directories with the following:

  • On Vim:

    • ~/.vim -> %USERPROFILE%\vimfiles
    • ~/.vimrc -> %USERPROFILE%\_vimrc
  • On Nvim:

    • ~/.local/share/nvim/site -> %USERPROFILE%\AppData\Local\nvim

Or other directories if you have configured/installed Vim/Nvim differently.

Vim 8 package manager

  1. mkdir -p ~/.vim/pack/git-plugins/start
  2. git clone --depth 1 https://github.com/jupyter-vim/jupyter-vim.git ~/.vim/pack/git-plugins/start/jupyter-vim

NeoVim

  1. mkdir -p ~/.local/share/nvim/site/pack/git-plugins/start
  2. git clone --depth 1 https://github.com/jupyter-vim/jupyter-vim.git ~/.local/share/nvim/site/pack/git-plugins/start/jupyter-vim

Pathogen

  1. cd ~/.vim/bundle
  2. git clone https://github.com/jupyter-vim/jupyter-vim.git

Vundle

  1. Plugin 'jupyter-vim/jupyter-vim'

Vim-Plug

  1. Plug 'jupyter-vim/jupyter-vim'

Vim+Python configuration

In order for this plugin to work, you must have Jupyter installed in the
Python environment that vim’s pythonx command uses. There are several possible strategies here.

Install jupyter into system python

When Vim is using your system python (the default on linux), you can just install Jupyter using a package manager. For example, on Ubuntu 18.04 and later:

  1. $ sudo apt install jupyter jupyter-core

Alternatively, you can use pip:

  1. $ sudo pip install jupyter

Dynamic mode

When Vim is compiled with Python in dynamic mode (+python3/dyn), you can point to the Python interpreter you wish to use in your .vimrc:

  1. if has('nvim')
  2. let g:python3_host_prog = '/path/to/python/bin/python3'
  3. else
  4. set pyxversion=3
  5. " OSX
  6. set pythonthreedll=/Library/Frameworks/Python.framework/Versions/3.6/Python
  7. " Windows
  8. set pythonthreedll=python37.dll
  9. set pythonthreehome=C:\Python37
  10. endif

Make sure to point it to a Python installation that has Jupyter installed.

Virtual environments

If either:

  • you use a Python environment manager such as virtualenv, and thus need
    Jupyter to be present no matter which environment is loaded from the shell
    you open vim from, or
  • you only use one Python environment but you don’t want to install Jupyter
    system-wide for whatever reason,

then the easiest way to meet the Jupyter requirement is to configure vim to
load a designated virtualenv at startup. This is just to allow vim to call the
Jupyter client; you can run your Jupyter server in whatever Python environment
you want. From Vim, run:

  1. :pythonx import sys; print(sys.version)

This will tell you whether pythonx is using Python 2 or Python 3. (Or, see
:help python_x if you’d like to tweak your pythonx settings.) Create a
virtualenv with that python version, for example

  1. $ virtualenv -p /usr/bin/python2.7 /path/to/my/new/vim_virtualenv

or

  1. $ virtualenv -p /usr/bin/python3 /path/to/my/new/vim_virtualenv

and then install Jupyter in that environment:

  1. $ source /path/to/my/new/vim_virtualenv/bin/activate
  2. $ pip install jupyter

Finally, tell vim to load this virtualenv at startup by adding these lines to
your vimrc:

  1. " Always use the same virtualenv for vim, regardless of what Python
  2. " environment is loaded in the shell from which vim is launched
  3. let g:vim_virtualenv_path = '/path/to/my/new/vim_virtualenv'
  4. if exists('g:vim_virtualenv_path')
  5. pythonx import os; import vim
  6. pythonx activate_this = os.path.join(vim.eval('g:vim_virtualenv_path'), 'bin/activate_this.py')
  7. pythonx with open(activate_this) as f: exec(f.read(), {'__file__': activate_this})
  8. endif

Jupyter configuration

First, we need to configure the jupyter console and qtconsole clients to
display output from other clients.

The config files can be found in in ~/.jupyter, if they don’t exist yet you
can generate them with:

  1. $ jupyter console --generate-config
  2. $ jupyter qtconsole --generate-config

Now you need to uncomment and change the following config options to True.

  • qtconsole

Edit this following file: ~/.jupyter/jupyter_qtconsole_config.py

  1. c.ConsoleWidget.include_other_output = True
  • console

Edit the following file: ~/.jupyter/jupyter_console_config.py

  1. c.ZMQTerminalInteractiveShell.include_other_output = True

Usage

To begin a session:

  1. $ jupyter qtconsole & # open a jupyter console window
  2. $ vim <your_script>.py

In vim: :JupyterConnect

Then, use :JupyterRunFile, or :[range]JupyterSendRange to execute lines of
code!

Code will be sent and executed as expected in the graphical jupyter qtconsole.
However, in the console version jupyter console, the result will only show after you press the Enter key.

By default, the following keybindings are defined:

  1. " Run current file
  2. nnoremap <buffer> <silent> <localleader>R :JupyterRunFile<CR>
  3. nnoremap <buffer> <silent> <localleader>I :PythonImportThisFile<CR>
  4. " Change to directory of current file
  5. nnoremap <buffer> <silent> <localleader>d :JupyterCd %:p:h<CR>
  6. " Send a selection of lines
  7. nnoremap <buffer> <silent> <localleader>X :JupyterSendCell<CR>
  8. nnoremap <buffer> <silent> <localleader>E :JupyterSendRange<CR>
  9. nmap <buffer> <silent> <localleader>e <Plug>JupyterRunTextObj
  10. vmap <buffer> <silent> <localleader>e <Plug>JupyterRunVisual
  11. " Debugging maps
  12. nnoremap <buffer> <silent> <localleader>b :PythonSetBreak<CR>

Set let g:jupyter_mapkeys = 0 in your .vimrc to prevent the default keybindings from being made.

  • vim-repl: Instead of connecting to, embed the REPL in vim
  • vimpyter: Integration with jupyter, even the notebook
  • vim-ipython-cell: send cell to jupyter via slime, requires terminal multiplexing instead of zeromq to communicate (higer level)
  • jupytext.vim: Convert notebook files to vim buffers

Info

Once we fell in love with Vim, we couldn’t bear having to jump back and forth
between the ipython/jupyter console and editor anymore. We modeled this simple
interface off of the ideas in
vim-ipython, but have pared down many
of the features, like the replication of the Jupyter console in a vim buffer,
to make the plugin much more ‘lightweight’.

Still a work in progress!

Troubleshooting

  • Make sure that you are running Vim 8 or higher with Python 3 support.
  • When on windows, you are probably running 64-bit Python. Make sure you also run a 64-bit version of Vim (the default install is 32-bit!).

Contributing

Please feel free to raise issues and pull requests on
the github repository.

Credits

We owe significant thanks to the original developer of this plugin:
Paul Ivanov.
It is far easier to update something that already works well than to forge
a new path from scratch.