pcall(require, 'module_with_error')
if not ok then
print("Module had an error")
else
mymod.func()
end
<
In contrast to |:source|, |require()| not only searches through all `lua/` directories
under |'runtimepath'|, it also caches the module on first use. Calling
`require()` a second time will therefore _not_ execute the script again and
instead return the cached file. To rerun the file, you need to remove it from
the cache manually first:
>lua
package.loaded['myluamodule'] = nil
require('myluamodule') -- read and execute the module again from disk
<
------------------------------------------------------------------------------
See also:
• |lua-module-load|
• |pcall()|
==============================================================================
Using Vim commands and functions from Lua *lua-guide-vimscript*
All Vim commands and functions are accessible from Lua.
------------------------------------------------------------------------------
Vim commands *lua-guide-vim-commands*
To run an arbitrary Vim command from Lua, pass it as a string to |vim.cmd()|:
>lua
vim.cmd("colorscheme habamax")
<
Note that special characters will need to be escaped with backslashes:
>lua
vim.cmd("%s/\\Vfoo/bar/g")
<
An alternative is to use a literal string (see |lua-literal|) delimited by
double brackets `[[ ]]` as in
>lua
vim.cmd([[%s/\Vfoo/bar/g]])
<
Another benefit of using literal strings is that they can be multiple lines;
this allows you to pass multiple commands to a single call of |vim.cmd()|:
>lua
vim.cmd([[
highlight Error guibg=red
highlight link Warning Error
]])
<
This is the converse of |:lua-heredoc| and allows you to include Vimscript
code in your `init.lua`.
If you want to build your Vim command programmatically, the following form can
be useful (all these are equivalent to the corresponding line above):
>lua
vim.cmd.colorscheme("habamax")
vim.cmd.highlight({ "Error", "guibg=red" })
vim.cmd.highlight({ "link", "Warning", "Error" })
<
------------------------------------------------------------------------------
Vimscript functions *lua-guide-vim-functions*
Use |vim.fn| to call Vimscript functions from Lua. Data types between Lua and
Vimscript are automatically converted:
>lua
print(vim.fn.printf('Hello from %s', 'Lua'))
local reversed_list = vim.fn.reverse({ 'a', 'b', 'c' })
vim.print(reversed_list) -- { "c", "b", "a" }
local function print_stdout(chan_id, data, name)
print(data[1])
end
vim.fn.jobstart('ls', { on_stdout = print_stdout })
<
This works for both |builtin-functions| and |user-function|s.
Note that hashes (`#`) are not valid characters for identifiers in Lua, so,
e.g., |autoload| functions have to be called with this syntax:
>lua
vim.fn['my#autoload#function']()
<
------------------------------------------------------------------------------
See also:
• |builtin-functions|: alphabetic list of all Vimscript functions
• |function-list|: list of all Vimscript functions grouped by topic
• |:runtime|: run all Lua scripts matching a pattern in |'runtimepath'|
• |package.path|: list of all paths searched by `require()`
==============================================================================
Variables *lua-guide-variables*
Variables can be set and read using the following wrappers, which directly
correspond to their |variable-scope|:
• |vim.g|: global variables (|g:|)
• |vim.b|: variables for the current buffer (|b:|)
• |vim.w|: variables for the current window (|w:|)
• |vim.t|: variables for the current tabpage (|t:|)
• |vim.v|: predefined Vim variables (|v:|)
• |vim.env|: environment variables defined in the editor session
Data types are converted automatically. For example:
>lua
vim.g.some_global_variable = {
key1 =