Home Explore Blog CI



neovim

4th chunk of `runtime/doc/lua.txt`
ee31aec8d533e18e0f866b2ec73b6fa5c21b067cbcd8610a0000000100000fa5
 the first call to `require()` for each module,
with subsequent calls returning the cached value without searching for, or
executing any script. For further details see |require()|.

For example, if 'runtimepath' is `foo,bar` and |package.cpath| was
`./?.so;./?.dll` at startup, `require('mod')` searches these paths in order
and loads the first module found ("first wins"): >
    foo/lua/mod.lua
    foo/lua/mod/init.lua
    bar/lua/mod.lua
    bar/lua/mod/init.lua
    foo/lua/mod.so
    foo/lua/mod.dll
    bar/lua/mod.so
    bar/lua/mod.dll
<
Note:

- Although 'runtimepath' is tracked, Nvim does not track current
  values of |package.path| or |package.cpath|. If you happen to delete some
  paths from there you can set 'runtimepath' to trigger an update: >vim
      let &runtimepath = &runtimepath

- Skipping paths from 'runtimepath' which contain semicolons applies both to
  |package.path| and |package.cpath|. Given that there are some badly written
  plugins using shell, which will not work with paths containing semicolons,
  it is better to not have them in 'runtimepath' at all.

==============================================================================
COMMANDS                                                        *lua-commands*

These commands execute a Lua chunk from either the command line (:lua, :luado)
or a file (:luafile) on the given line [range]. As always in Lua, each chunk
has its own scope (closure), so only global variables are shared between
command calls. The |lua-stdlib| modules, user modules, and anything else on
|package.path| are available.

The Lua print() function redirects its output to the Nvim message area, with
arguments separated by " " (space) instead of "\t" (tab).

                                                                  *:lua=* *:lua*
:lua {chunk}
    Executes Lua chunk {chunk}. If {chunk} starts with "=" the rest of the
    chunk is evaluated as an expression and printed. `:lua =expr` and `:=expr`
    are equivalent to `:lua print(vim.inspect(expr))`.

    Examples: >vim
        :lua vim.api.nvim_command('echo "Hello, Nvim!"')
<    To see the Lua version: >vim
        :lua print(_VERSION)
<    To see the LuaJIT version: >vim
        :lua =jit.version
<
:{range}lua
    Executes buffer lines in {range} as Lua code. Unlike |:source|, this
    always treats the lines as Lua code.

    Example: select the following code and type ":lua<Enter>" to execute it: >lua
        print(string.format(
            'unix time: %s', os.time()))
<
                                                                *:lua-heredoc*
:lua << [trim] [{endmarker}]
{script}
{endmarker}
    Executes Lua script {script} from within Vimscript. You can omit
    [endmarker] after the "<<" and use a dot "." after {script} (similar to
    |:append|, |:insert|). Refer to |:let-heredoc| for more information.

    Example: >vim
        function! CurrentLineInfo()
        lua << EOF
        local linenr = vim.api.nvim_win_get_cursor(0)[1]
        local curline = vim.api.nvim_buf_get_lines(0, linenr - 1, linenr, false)[1]
        print(string.format('Line [%d] has %d bytes', linenr, #curline))
        EOF
        endfunction
<
    Note that the `local` variables will disappear when the block finishes.
    But not globals.

                                                                      *:luado*
:[range]luado {body}
    Executes Lua chunk "function(line, linenr) {body} end" for each buffer
    line in [range], where `line` is the current line text (without <EOL>),
    and `linenr` is the current line number. If the function returns a string
    that becomes the text of the corresponding buffer line. Default [range] is
    the whole file: "1,$".

    Examples: >vim
        :luado return string.format("%s\t%d", line:reverse(), #line)

        :lua require"lpeg"
        :lua -- balanced parenthesis grammar:
        :lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
        :luado if bp:match(line) then return "=>\t"

Title: Lua Module Loading Details and Commands
Summary
This section details the caching mechanism of `require()` in Lua and provides an example of how modules are searched and loaded based on 'runtimepath' and 'package.cpath'. It also covers the various commands used to execute Lua code within Vim, including `:lua`, `:{range}lua`, `:lua <<`, and `:luado`, explaining their syntax, functionality, and providing usage examples.