Map a Lua function directly to a Vimscript function

I have some Lua function in a module, which in Lua I might call as require('a/b').zap(x, y, z).

Is there some way to “map” or “alias” this Lua function directly to a Vimscript function? Currently the best I could do was to use luaeval:

function! Zap() abort
    call luaeval('require("a/b").zap(_A[1], _A[2], _A[3])', [x, y, z])
endfunction

But this seems a little clunky. It would be great if I could do something like this instead:

call nvim_map_luafunc("Zap", {'module': 'a/b', 'name': 'zap'})

Or even (from Lua):

vim.api.map_luafunc("Zap", function() ... end)

I could probably write my own version of this without too much difficulty, but it would be nice if this were built into Neovim somehow. Does this exist and I missed it in the manual, or is this effectively a feature request?

1 Like

You can use

vim.g.Zap = function() ... end 
1 Like

That’s pretty clever, make use of the g: implicit prefix so that Zap() is just g:Zap(). It wouldn’t work inside functions though, where the default prefix is l: instead of g:.

However I did figure out a better solution than the luaeval() thing above:

v:lua.require(module)[funcname](args)
1 Like