neovim / pynvim

Python client and plugin host for Nvim
http://pynvim.readthedocs.io/en/latest/
Apache License 2.0
1.48k stars 118 forks source link

Unable to configure settings through lua file #481

Closed AckslD closed 2 years ago

AckslD commented 3 years ago

I'm trying to use pynvim for testing a plugin but I am unable to run a nvim with the configuration I want.

Let's say I have the following minimal configuration-file:

-- tmp.lua
vim.g.mapleader = ','

Now, consider the following normal execution of nvim:

/bin/env nvim -u NONE +"luafile /path/to/tmp.lua" +"echo g:mapleader"

which as exepcted shows ,.

However, if I run the following

from pynvim import attach
nvim = attach('child', argv=[
    "/bin/env",
    "nvim",
    "--embed",
    "--headless",
    "-u",
    "NONE",
    "+\"luafile /path/to/tmp.lua\"",
])
print(nvim.command_output("echo g:mapleader"))

I get

Traceback (most recent call last):
  File "/path/to/tmp.py", line 11, in <module>
    print(nvim.command_output("echo g:mapleader"))
  File "/usr/lib/python3.9/site-packages/pynvim/api/nvim.py", line 291, in command_output
    return self.request('nvim_command_output', string)
  File "/usr/lib/python3.9/site-packages/pynvim/api/nvim.py", line 182, in request
    res = self._session.request(name, *args, **kwargs)
  File "/usr/lib/python3.9/site-packages/pynvim/msgpack_rpc/session.py", line 102, in request
    raise self.error_wrapper(err)
pynvim.api.common.NvimError: Vim(echo):E121: Undefined variable: g:mapleader

Is this an issue with pynvim or am I doing something wrong?

justinmk commented 2 years ago
nvim = attach('child', argv=[
    "/bin/env",
    "nvim",
    "--embed",
    "--headless",
   ...

See :help --embed.

--embed is for UIs (or clients that want UI events). In that case nvim waits for nvim_ui_attach() before continuing, and that means it doesn't run the user config (-u) nor any other commands until then.

print(nvim.command_output("echo g:mapleader"))

btw, nvim_command_output() is deprecated, use nvim_exec() instead.

Summary

Just remove --embed from your start args. Or call nvim_ui_attach if you want UI events.