VimL to Lua and back

VimL to Lua and back

There are different reasons for having VimScript and Lua code interact, such as calling existing functions, setting variables to configure other plugins, or Neovim. Below are descriptions of techniques for different kinds of interactions between VimScript and Lua.

From Lua

Accessing VimL Variables

There are a number of variables with different scopes. Depending on the scope of the variable, there are different APIs for accessing the values.

  1. Global variables (echo g:something):

    local a = vim.api.nvim_get_var('something')
    vim.api.nvim_set_var('something', 'value')
  2. Vim variables (echo v:servername):

    print(vim.api.nvim_get_vvar("servername"))
  3. And for general expression evaluation, which can include accessing variables:

    local servername = vim.api.nvim_eval("v:servername")

Calling VimL Functions

vim.api.nvim_call_function(...)

From VimScript

Calling Lua Code from VimL

nvim_execute_lua(...)

Starting in Neovim 0.5.0, it is possible to invoke global Lua functions directly from VimL:

v:lua.luafunc(arg1, arg2)