Pynvim

0.6.0 · active · verified Thu Apr 16

Pynvim is the official Python client and plugin host for Neovim, enabling Python developers to interact with Neovim instances and write powerful remote plugins. Currently at version 0.6.0, it is actively maintained with regular releases addressing bug fixes, performance improvements, and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a running Neovim instance using a Unix domain socket and interact with it. It executes a command, reads from the current buffer, and sets a global Neovim variable. Ensure Neovim is started with `nvim --listen /tmp/nvim.sock` (or similar for Windows/TCP) for the client to connect.

# Start Neovim in a terminal with: nvim --listen /tmp/nvim.sock

import os
from pynvim import attach

# Connect to a running Neovim instance via socket
# Ensure Neovim is running with `--listen /tmp/nvim.sock`
# For local testing, you might use os.environ.get('NVIM_LISTEN_ADDRESS')
try:
    nvim = attach('socket', path=os.environ.get('NVIM_LISTEN_ADDRESS', '/tmp/nvim.sock'))
    
    # Execute a Neovim command
    nvim.command('echo "Hello from pynvim!"')
    
    # Get and print the current buffer content
    current_buffer = nvim.current.buffer
    print(f"First line of current buffer: {current_buffer[0]}")
    
    # Set a Neovim variable
    nvim.vars['pynvim_test_var'] = 'Python was here'
    print(f"Neovim variable 'pynvim_test_var': {nvim.eval('g:pynvim_test_var')}")

except Exception as e:
    print(f"Could not connect to Neovim or encountered an error: {e}")
    print("Make sure Neovim is running with '--listen /tmp/nvim.sock' or NVIM_LISTEN_ADDRESS is set.")

view raw JSON →