Introduction to vimrunner-python

07 Jan 2015 in TIL

Note: I'd advise installing python libraries via virtualenv

I wanted to try out vimrunner-python but the only docs available are more API docs than usable documentation.

Here's a 60 second guide to getting it running:

Make sure your install of vim supports clientserver:

bash
[[ -n "`vim --version | grep client`" ]] && echo "Installed" || echo "Not installed"

If it doesn't, read this post

Once it does, start an instance of vim in server mode:

bash
vim --servername this_is_our_name

Create a python file with the following contents to talk to it:

python
import vimrunner
s = vimrunner.Server("this_is_our_name")
# initialize vim server
client = vimrunner.vimrunner.Client(s)
client.type("IHelloWorld<esc>");

Run the python file, see the words appear.

Useful functions

I used client.type() to send raw commands to the vim instance, but vimrunner-python provides a few useful abstractions.

  • client.insert("Hello World") - Will insert "Hello World" at the current location and leave you in insert mode
  • client.normal("dd") - Will send "dd" in normal mode, cutting the current line into a buffer