DEV Community

Cover image for A concrete Python 'Hello World' with the Neovim API
Precious Chicken
Precious Chicken

Posted on • Originally published at preciouschicken.com on

2 2

A concrete Python 'Hello World' with the Neovim API

Neovim's API is typically called through a Remote Procedure Call (RPC) using the MessagePack-RPC specification. The neovim documentation provides a 'Hello World' example of how to interface with the Neovim API; this post is a worked example of how to implement this - more of a note to self than anything else. I'm running Neovim v 0.6.0 on Manjaro 21.2.0 Qonos linux.

First we need to determine the servername that Neovim has set on startup. Fire up an instance of nvim from the terminal with the command:

nvim -c "echo v:servername"
Enter fullscreen mode Exit fullscreen mode

The -c command option used above causes neovim to run a command on opening, in this case to print out the servername of the instance. Resultantly we should see the servername of the instance in the statusline; it should look a little like: /tmp/nvimt7lIuM/0.

We can now use this address to command the neovim instance remotely. Create a file named nvim-hw-api.py and paste the following - altering the path to match the servername we found above:

from pynvim import attach
nvim = attach('socket', path='/tmp/nvimt7lIuM/0')
nvim.command('echo "hello world!"')
Enter fullscreen mode Exit fullscreen mode

At the terminal run (assuming Python3 is installed):

python3 nvim-hw-api.py
Enter fullscreen mode Exit fullscreen mode

And if all works as forecast we should see hello world! appear in the statusline of the neovim instance!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay