All Projects → jacobsimpson → Nvim Example Python Plugin

jacobsimpson / Nvim Example Python Plugin

Licence: unlicense
A simple Neovim Python plugin suitable as a template.

Labels

Projects that are alternatives of or similar to Nvim Example Python Plugin

Dotfiles
💻 Use command line interface manager for macOS configuration.
Stars: ✭ 97 (-15.65%)
Mutual labels:  neovim
Dotfiles
My dotfiles for Archlinux and Windows
Stars: ✭ 1,419 (+1133.91%)
Mutual labels:  neovim
Ubuntu On Steroids
Developer setup & configuration guide for Ubuntu.
Stars: ✭ 111 (-3.48%)
Mutual labels:  neovim
Vim current word
Plugin highlighting word under cursor and all of its occurences
Stars: ✭ 100 (-13.04%)
Mutual labels:  neovim
Denops.vim
🐜 An ecosystem of Vim/Neovim which allows developers to write plugins in Deno
Stars: ✭ 102 (-11.3%)
Mutual labels:  neovim
Fzf
🌸 A command-line fuzzy finder
Stars: ✭ 40,965 (+35521.74%)
Mutual labels:  neovim
Asyncrun.vim
🚀 Run Async Shell Commands in Vim 8.0 / NeoVim and Output to the Quickfix Window !!
Stars: ✭ 1,332 (+1058.26%)
Mutual labels:  neovim
Nvim Autopairs
autopairs for neovim written by lua
Stars: ✭ 112 (-2.61%)
Mutual labels:  neovim
Nvim Toggleterm.lua
A neovim lua plugin to help easily manage multiple terminal windows
Stars: ✭ 102 (-11.3%)
Mutual labels:  neovim
Polka
🐢 dotless files - plz no steel (only cardboard) (sponsored by https://github.com/buffet/kiwmi, plz star & contribute)
Stars: ✭ 109 (-5.22%)
Mutual labels:  neovim
Vim Follow My Lead
Vim plugin for showing all your <Leader> mappings in a readable table including the descriptions.
Stars: ✭ 100 (-13.04%)
Mutual labels:  neovim
Neovim Fuzzy
Fuzzy file finding for neovim
Stars: ✭ 103 (-10.43%)
Mutual labels:  neovim
Neovim
Vim-fork focused on extensibility and usability
Stars: ✭ 49,389 (+42846.96%)
Mutual labels:  neovim
Close Buffers.vim
📖 Quickly close (bdelete) several buffers at once 📕
Stars: ✭ 99 (-13.91%)
Mutual labels:  neovim
Coc Html
Html language server extension for coc.nvim.
Stars: ✭ 113 (-1.74%)
Mutual labels:  neovim
Mac Bootstrap
💻 Provision a new Mac for web development with dotfiles + Fish/Zsh, Neovim, and Tmux
Stars: ✭ 96 (-16.52%)
Mutual labels:  neovim
Nmux
A multiplexer for Neovim processes
Stars: ✭ 107 (-6.96%)
Mutual labels:  neovim
Nvim Treesitter Context
Show code context
Stars: ✭ 113 (-1.74%)
Mutual labels:  neovim
Nvim Ts Rainbow
🌈 Rainbow parentheses for neovim using tree-sitter 🌈
Stars: ✭ 108 (-6.09%)
Mutual labels:  neovim
Toast.vim
🍞 Toast! A colorful, medium-contrast color scheme with full Vim and Neovim support and automatic light and dark variants. Easy to read without frying your retinae.
Stars: ✭ 108 (-6.09%)
Mutual labels:  neovim

Example Neovim Python Plugin

Introduction

As part of the changes included in Neovim there is a new plugin model where plugins are separate processes which Neovim communicates to using the MessagePack protocol.

Since plugins are distinct from the Neovim process, it is possible to write plugins in many languages.

This is a minimal example of a Python plugin. When you want to create a new Python plugin, you should be able to (and feel free to) copy this repository, rename a couple files, include the plugin in your Vim config and see something happen.

Installing

Downloading

The intention of this repository is to make it quick and easy to start a new plugin. It is just enough to show how to make the basics work.

git clone --depth 1 https://github.com/jacobsimpson/nvim-example-python-plugin ~/.vim/bundle/nvim-example-python-plugin
rm -Rf ~/.vim/bundle/nvim-example-python-plugin/.git

Configuring Vim

I use NeoBundle so this is an example of how to load this plugin in NeoBundle.

" Required:
call neobundle#begin(expand('~/.vim/bundle/'))

    " Let NeoBundle manage NeoBundle
    " Required:
    NeoBundleFetch 'Shougo/neobundle.vim'

    " You probably have a number of other plugins listed here.

    " Add this line to make your new plugin load, assuming you haven't renamed it.
    NeoBundle 'nvim-example-python-plugin'
call neobundle#end()

If you use vim-plug, you can add this line to your plugin section:

Plug 'jacobsimpson/nvim-example-python-plugin'

After running :PlugInstall, the files should appear in your ~/.config/nvim/plugged directory (or whatever path you have configured for plugins).

Python Version

This plugin code works with Python 2. You can make it work with Python 3 by changing the rplugin/python directory to be rplugin/python3. See the python-client remote plugin documentation for more information.

Initializing Vim with Remote Plugin

The next thing to do is to initialize the manifest for the Python part of the plugin. The manifest is a cache that Vim keeps of the interface implemented by the Python part of the plugin. The functions and commands it implements.

To initialize the manifest, execute:

:UpdateRemotePlugins

NOTE: After initializing the manifest, you must restart neovim for the python functions be be available.

Testing the New Plugin

There is some VimL in the plugin that will print when Neovim is starting up:

Starting the example Python Plugin

That will confirm that the VimL portions of the plugin are loading correctly.

There is a function defined in the VimL portion of the plugin which echos some text. You can execute the function like this:

:exec DoItVimL()

Now that the manifest is initialized, it should be possible to invoke the function defined in the Python part of the plugin. Look in __init__ to see the implementation.

:exec DoItPython()

Development

On it's own, this plugin doesn't do anything interesting, so the expectation is that you will want to modify it.

Debugging

In order to take advantage of the Python REPL and make it easier to test changes in your Python code, I usually take the following steps:

  1. Open a Neovim instance.
  2. Open a terminal inside Neovim. (:term)
  3. Start the Python, or IPython, interpreter in the Neovim terminal. (python, ipython)
  4. Execute this code in the Python interpreter:
import neovim
import os

nvim = neovim.attach('socket', path=os.environ['NVIM_LISTEN_ADDRESS'])

At this point, you can either execute commands directly against Neovim, to test the behavior of the interface:

nvim.current.buffer.name

or load your own plugin class and work with it directly.

%run "rplugin/python/nvim-example-python-plugin.py"
m = Main(nvim)
m.doItPython([])

Plugin Interface Changes

Neovim includes a step where the interface of the remote plugin is cached for Neovim, so that Neovim knows what functions and commands your plugin is making available without having to wait while the external process containing the plugin is started.

:UpdateRemotePlugins

Run this command for every change in the plugin interface. Without this, you may see errors on from Neovim telling you methods are missing from your plugin. Or the new functionality you are trying to add just won't work.

Troubleshooting

Refreshing the Manifest File

For each change to the interface of the Python plugin, that is to say, any alterations to the @neovim decorators, you need to update Neovim's manifest file:

:UpdateRemotePlugins

Restart Neovim after you update to make the changes take effect.

If there is a syntax error in the Python file, it may result in the plugin not loading. There may be no visible error. If you run the update command, and the commands and functions defined in the remote plugin are not available, the next useful troubleshooting step is to load your plugin directly in a Python interpreter to see if it works.

Python Client Log File

Define this environment variable to get output logged from your Python client.

export NVIM_PYTHON_LOG_FILE=${HOME}/.nvim-python.log

The output files will have a number appended, and should be visible with this:

ls ${HOME}/.nvim-python.log*

Neovim Log File

ls ~/.nvimlog

Neovim Library

One problem I encountered when I was first getting started was the Python neovim module was not installed on my system. I didn't see any great errors that lead me to that conclusion, so it is worth checking:

python -c "import neovim"

Should execute without an error.

References

The Neovim docs for remote plugins. It's a little sparse, but captures the core detail.

The Neovim Python client is the Python API that wraps the MessagePack protocol Neovim uses to communicate with remote plugins. If you are looking for more information on how to use the vim parameter to the main object to control Neovim, this is the place to go.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].