neovim-client

Neovim client library

Usage no npm install needed!

<script type="module">
  import neovimClient from 'https://cdn.skypack.dev/neovim-client';
</script>

README

neovim-client

Build Status

Installation

npm install --global neovim-client

Usage

This package exports a single attach() function which takes a pair of write/read streams and invokes a callback with a Nvim API object. This is similar to node-msgpack5rpc, but it provides an automatically generated API.

Additionally, a Promise-based API may be obtained using require('neovim-client/promise').

Examples:

var cp = require('child_process'),
    attach = require('neovim-client'),
    nvim_proc = cp.spawn('nvim', ['-u', 'NONE', '-N', '--embed'], {})

attach(nvim_proc.stdin, nvim_proc.stdout, function(err, nvim) {

  nvim.on('request', function(method, args, resp) {
    // handle msgpack-rpc request
  })

  nvim.on('notification', function(method, args) {
    // handle msgpack-rpc notification
  })

  nvim.command('vsp', function(err, res) {
    nvim.getWindows(function(err, windows) {
      console.log(windows.length)  // 2
      console.log(windows[0] instanceof nvim.Window) // true
      console.log(windows[1] instanceof nvim.Window) // true
      nvim.setCurrentWindow(windows[1], function(err, res) {
        nvim.getCurrentWindow(function(err, win) {
          console.log(win.equals(windows[1]))  // true
          nvim.quit()
          nvim.on('disconnect', function() {
            console.log("Nvim exited!")
          })
        })
      })
    })
  })
})

or, using Promises:

var cp = require('child_process'),
    attach = require('neovim-client/promise'),
    nvim_proc = cp.spawn('nvim', ['-u', 'NONE', '-N', '--embed'], {}),
    nvim

attach(nvim_proc.stdin, nvim_proc.stdout)
.then(function(_nvim) {
    nvim = _nvim
    return nvim.command('vsp')
})
.then(function(res) {
    return nvim.getWindows()
})
.then(function(windows) {
    console.log(windows.length)  // 2
    console.log(windows[0] instanceof nvim.Window) // true
    console.log(windows[1] instanceof nvim.Window) // true
    return nvim.setCurrentWindow(windows[1])
    .then(function(res) {
        return nvim.getCurrentWindow()
    })
    .then(function(win) {
        console.log(win.equals(windows[1]))
        nvim.on('disconnect', function() {
            console.log("Nvim exited!")
        })
        nvim.quit()
    })
})
.catch(function(err) {
    console.error(err)
    nvim.quit()
})

Methods are attached to buffers, windows and tabpages according to the msgpack-rpc name:

nvim.getCurrentBuffer(function(err, buf) {
  console.log(buf instanceof nvim.Buffer)  // true
  buf.getLineSlice(0, -1, true, true, function(err, lines) {
    console.log(lines)  // ['']
    buf.setLineSlice(0, -1, true, true, ['line1', 'line2'], function(err) {
      buf.getLineSlice(0, -1, true, true, function(err, lines) {
        console.log(lines)  // ['line1', 'line2']
      })
    })
  })
})

A typescript declaration file is available as documentation of the API and also for typescript users that seek to use this library. Note that the interfaces are automatically generated at a certain point in time, and may not correspond exactly to the API of your installed Nvim.