2

Is there a way to control the opened instance of Vim from outside?

Let me clarify, I saw many posts about controlling an external program from Vim by simply executing commands on special key strokes occasions. But what I need is the complete opposite, sending instructions to modify how things look in the currently opened instance.

Here's what I want to accomplish: I am trying to make a webapp WYSIWYG, what you write in the textarea is processed and rendered in a box, that requires a lot of typing and modification like writing a program, but the original textarea has not a lot under the hood.

That's why I want to use Vim, and not only should I be able to use Vim to send information to the browser (which is possible I already know) I should also be able to send some messages to Vim from the browser because the box is also editable and modifies the textarea.

What would be the easiest way to accomplish that? As a start I want to know if changing the caret position (line) from an external signal is doable.

1
  • Is Neovim a suitable option? It offers a full RPC API to control running instances and should let you do anything you can do directly in nvim itself.
    – Holloway
    Jun 21, 2022 at 9:41

2 Answers 2

4

For Vim you can use the --remote-send flag. It takes as an argument the Vim command that you would like to be executed.

For example:

vim --remote-send ":echo 'hello'<CR>"

Will echo hello into your running Vim.

You can execute any command you would like instead of :echo this way.

You can use --servername to specify the remote instance of Vim which should receive the command.

Remark: you need Vim to be compiled with the +clientserver option. More information with: :help clientserver

5
  • I tried but I have this error Unknown option argument: "--remote-send"
    – vdegenne
    Jun 20, 2022 at 21:37
  • Which version of gVim do you have? Did you tried with Vim or gVim? Jun 20, 2022 at 22:39
  • I have tried successfully on both Windows and Linux with both vim and gvim. I noticed that on Linux vim must be launched with --servername VIM in order to received the command of vim (the default vim servername seems to be the empty string: "") Jun 21, 2022 at 4:35
  • 1
    Unknown option argument: "--servername". I am using vim 8.1 in wsl I also tried installing vim in windows but the commands are unknown. Is it because it was not compiled with +clientserver ?
    – vdegenne
    Jun 21, 2022 at 7:42
  • Indeed you need the +clientserver option for the --remote-xxx commands to work. Jun 21, 2022 at 8:08
2

You can also use a JSON channel to send commands to Vim. For more information about this refer to https://vimhelp.org/channel.txt.html#channel-commands. Note that Vim can only be a client and not a server. So the Vim instance needs to open a channel in JSON mode to the external server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.