29

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?

3 Answers 3

30

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.

2
  • 1
    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. Commented Mar 26, 2020 at 11:03
  • I wish there was a way to get this to work with open -g -a /Applications/MacVim.app --args --remote-silent file1.txt file2.txt bit. I end up getting separate windows for each file. Commented Dec 29, 2023 at 4:42
9

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.

2
  • I tried doing this, but I only ended up with a new instance of vim each time I ran vim --remote-silent <file>. I am running neovim, if that has something to do with it? Commented Mar 9 at 16:49
  • Additionally, you can use --remote-silent-tab instead of --remote-silent.
    – Bass
    Commented May 27 at 16:05
2

In addition to @craigp's answer, you can also open a file in a remote tab.

vim --servername SERVER --remote-tab FILE

1

Your Answer

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

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