Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send commands to vim programmatically

Tags:

javascript

vim

I'd like to build an interface in Javascript where I collect key presses and send them to a remote vim instance and have the instance report back any changes. I'm quite confident I can implement all of the browser-server communication and javascript --- I'm just not sure how to pass the data to vim and how to have it talk back to me.

Any pointers would be appreciated! Thanks

Edit: An initial implementation can be found on Github.

like image 313
Honza Pokorny Avatar asked Dec 14 '11 16:12

Honza Pokorny


1 Answers

Take a look at TextAid (specifically the Perl server). Maybe you'll find useful ideas.

--RE-EDIT--

Since I like your idea I've looked a bit further.

Supposing Vim is compiled with +clientserver, you can launch vim like this:

$ vim --servername MYSERVER /path/to/tempfile`

on your remote box. Vim is launched in server mode and you can send it commands like:

$ vim --servername MYSERVER --remote-send 'ihello<Esc>'

to have:

hello

on the first line, if you send:

$ vim --servername MYSERVER --remote-send 'A world<Esc>'

you obtain:

hello world

If you send:

$ vim --servername MYSERVER --remote-send 'yy5p'

you obtain:

hello world
hello world
hello world
hello world
hello world

If you send:

$ vim --servername MYSERVER --remote-send ':%s/hello/goodbye cruel<CR>'

you obtain:

goodbye cruel world
goodbye cruel world
goodbye cruel world
goodbye cruel world
goodbye cruel world

which is kind of cool but you need to send back the new state of the buffer which you would do by writing the temporary file and sending its content back to the client after each command. That means a lot of read/write on your server. One could also write a macro that sends the content of the buffer to some external process on each "change".

--EDIT--

If you had followed the link instead of dismissing my answer you would have seen that the extension above works by sending the content of the textarea to the Perl server via HTTP.

I don't think it matters at all if Vim is local or on a distant server.

The server writes it into a temporary file that is then opened with Vim.

When Vim writes the file, its content is then sent back to the extension by the server.

Your piece of JS would do what the extension does: grab the content of the textarea, send it via an AJAX POST request to your server, wait for an answer and update the content of the textarea with it.

Actually, I think that you could even use the script above as is. Or at least take it as a starting point for your own app.

like image 61
romainl Avatar answered Oct 23 '22 08:10

romainl