23

Some applications have the notion of a "session", where you can run a command to load a file in an existing instance of an application.

For example, when I type:

$ firefox http://vi.stackexchange.com

Firefox re-uses an existing Firefox process, rather than creating a new one.

Is this possible with Vim?

24

You need vim compiled with +clientserver, and then you can use the command:

vim --servername SERVER to start a vim instance, and

vim --servername SERVER --remote FILE to open the file in the named vim instance.


macOS only

MacVim runs a server by default - you can use

mvim --remote-tab-silent to open a file in a new tab in your existing MacVim instance, or

mvim --remote-silent to open the file in a new buffer in the same tab.

| improve this answer | |
  • if you want this in NeoVim, which doesn't support --servername, install Neovim-Remote by doing pip3 install neovim-remote and then you can do nvr --remote filename.txt from a seperate shell to open filename in the other, or use nvr -l filename.txt from a :terminal buffer inside vim, to open the file in the other buffer (rather than opening vim within vim, as you otherwise might). Similarly, git can be configured to use the vim instance calling it, rather than spawning vim within vim. – TamaMcGlinn Mar 26 at 11:03
4

While @craigp's answer is correct, I found it most convenient to simply add --remote-silent without worrying about server names,
(this assumes you don't want to address named vim instances).

This will start the server, or use one if its not already started.

It can be called like this:

gvim --remote-silent '+cal cursor(line,col)' some_file

eg:

gvim --remote-silent '+cal cursor(102,4)' src/code.c

I ended up needing to use a different method of moving the cursor because of this remote initialization.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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