24

in vim I can do vim --servername foo and vim --servername foo --remote-silent bar.txt to open files in one instance of vim from several terminal windows (via tmux or whatever). How is the same thing achieved in NeoVim?

5 Answers 5

14

Neovim remote is a python script for controlling neovim processes. It has the vim style --servername and --remote options. This is what I use now for my multiple-terminal work environment.

1
  • 8
    It worth mentioning that this plugin does not work for Windows platform.
    – jdhao
    Mar 26, 2019 at 12:08
11

As far as I know vim has this feature because the GUI vim (gvim) uses the graphical X server. I am aware that non GUI Vim can also use the --servername argument but only when X11 is running and also by accessing to it. On the other hand, neovim has no graphical support; for that reason, I don't think that the --servername option can work (unless the option would have been fully rewritten in another way) since I would find strange to have a console program accessing the X11 server only for that purpose.

0
10

As mentioned, all of the clientserver features were removed in Neovim. This is because they use the X11 shared memory and Neovim removed X11 support.

However, if I use :help --servername in Neovim I end up in the documentation for the serverstart() function:

serverstart([{address}])                                serverstart()
                Opens a named pipe or TCP socket at {address} for clients to
                connect to and returns {address}. If no address is given, it
                is equivalent to:
                        :call serverstart(tempname())
                $NVIM_LISTEN_ADDRESS is set to {address} if not already set.
                                                        --servername
                The Vim command-line option --servername can be imitated:
                        nvim --cmd "let g:server_addr = serverstart('foo')"

This will make a socket in /tmp/; see :echo v:servername for the path. It's probably more useful to use a more deterministic address (e.g. serverstart('/tmp/nvim-remote'); although you'll probably want to wrap this in a function, as it won't append a number like Vim's --serverstart does if this already exists).

You can send commands with the RPC API. As far as I can find there is no way to send commands to this with just Neovim (meh). You'll need to install one of the API clients. For example with the reference Python API client (which is also what Neovim remote uses):

import neovim
nvim = neovim.attach('socket', path='/tmp/nvim-remote')
try:
    nvim.command(':e foo')
finally:
    nvim.quit()

You can probably send the same bytes with a echo ... >> /tmp/nvim-remove shell command, but the RPC API uses a somewhat non-trivial non-text protocol ("MessagePack RPC") and figuring out which bytes to send exactly was a bit too much effort for this post ;-)

See :help rpc for the full details on Neovim's RPC API.


N.B. :help remote.txt as well as many other help items (such as help server2client() and many more) still exists in Neovim, but none of that works. It looks like whoever removed this feature did a pretty sloppy job.

4
  • This is an excellent answer. It enabled me to write my own, simple, windows compatible version of neovim-remote for running in :terminal. Given this information it was wonderfully easy.
    – David
    Oct 18, 2019 at 15:37
  • 1
    @David could you share it? Mar 25, 2020 at 8:53
  • 1
    The python program nvr solves this problem neatly (works on windows as well as linux): github/mhinz/neovim-remote Mar 25, 2020 at 10:00
  • 2
    @TamaMcGlinn here is my little script: gist.github.com/dhazel/4858340f66e18cb7e12cd2ec8f8ca8d7 . Thanks for the tip about nvr. It's been several years since I looked at it.
    – David
    Mar 26, 2020 at 23:07
2

For Windows I propose the following solution:

Add the following line to you ginit.vim file:

silent execute "!echo " . v:servername . " > servername.txt"

Remark: The ginit.vim file is to be found or created in %LocalAppData%\nvim (e.g.: on Windows 10 for John Doo: C:\Users\JDoo\AppData\Local\nvim

Start nvim-qt using the following batch file:

@setlocal
@pushd %~dp0
@if not exist servername.txt goto nvimqt
@%~dp0\nvim.py %*
@if [%errorlevel%]==[0] goto :end

@:nvimqt
@%~dp0\bin\nvim-qt.exe %*
@del %~dp0\servername.txt

:end
@popd

That make use of the following Python script:

import os
import sys
import neovim


def main():
    servername_path = "servername.txt"

    if not os.path.isfile(servername_path):
        return 1

    with open(servername_path, mode="rt") as f:
        line = f.readline()
        servername = line.strip("\r\n ")

    try:
        nvim = neovim.attach("socket", path=servername)
    except Exception:
        print("Neovim not found")
        os.remove(servername_path)
        return 1

    if len(sys.argv) <= 1:
        return

    path = sys.argv[1]
    nvim.command(':e %s' % path)

    nvim.close()


if __name__ == "__main__":
    ret = main()
    sys.exit(ret)
1
  • Oh great idea! Even in terminal this is much simpler for latex work. Just add tex auto command to save servername then nvr with the servername. This is in place of starting a nvim instance with a specific servername, which appears to also conflict with built in lsp server stuff. (Maybe the latter is a bug.)
    – Yasha
    Sep 25, 2021 at 15:08
2

An update to the answers. Neovim now supports some limited use of --remote and friends. I think more will be coming in the future. See https://neovim.io/doc/user/remote.html

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.