vim-driver provides a way for out-of-process programs to remotely instruct the behavior of vim instances.
$ mkdir my-project && cd my-project
$ yarn init
$ yarn add vim-driver
$ vim src/index.js
This example vim-driver server instructs all connecting vim clients to type out the words: Hello, vim-driver!
.
const path = require('path');
const {Server} = require('vim-driver');
const HOST = '127.0.0.1';
const PORT = 8765;
const TYPING_SPEED = 50;
const main = async () => {
const server = new Server();
await server.listen(HOST, PORT);
server.subscribe({
onConnect: async remote => {
await remote.edit('Demo.txt');
await type(remote, 'Hello, vim-driver!');
},
});
};
const type = async (remote, string) => {
// Enter insert mode
await remote.call('feedkeys', ['i']);
// Type input string
for (const letter of string.split('')) {
await remote.call('feedkeys', [letter]);
await sleep(TYPING_SPEED);
}
// Exit insert mode
await remote.call('feedkeys', ['\\<ESC>']);
};
const sleep = time => {
return new Promise(resolve => {
setTimeout(resolve, time);
});
};
main();
node src/index.js
$ cd VIM_DRIVER_PROJECT_ROOT
$ vim
:source dist/VimDriverClient.vim
:call VimDriverClient#open("127.0.0.1", 8765)
You can use vim-driver to run automated headless tests for your vim plugin!
See src/__tests__/HeadlessRemoteClient.test.js
for examples.
$ yarn run test
$ yarn run example