Home Explore Blog CI



neovim

4th chunk of `runtime/doc/lua-guide.txt`
d59fba58fb1ad0902c6e18f58b6779ce962ee86855c291f90000000100000fff

------------------------------------------------------------------------------
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 = "value",
      key2 = 300
    }

    vim.print(vim.g.some_global_variable)
    --> { key1 = "value", key2 = 300 }
<
You can target specific buffers (via number), windows (via |window-ID|), or
tabpages by indexing the wrappers:
>lua
    vim.b[2].myvar = 1               -- set myvar for buffer number 2
    vim.w[1005].myothervar = true    -- set myothervar for window ID 1005
<
Some variable names may contain characters that cannot be used for identifiers
in Lua. You can still manipulate these variables by using the syntax
>lua
    vim.g['my#variable'] = 1
<
Note that you cannot directly change fields of array variables. This won't
work:
>lua
    vim.g.some_global_variable.key2 = 400
    vim.print(vim.g.some_global_variable)
    --> { key1 = "value", key2 = 300 }
<
Instead, you need to create an intermediate Lua table and change this:
>lua
    local temp_table = vim.g.some_global_variable
    temp_table.key2 = 400
    vim.g.some_global_variable = temp_table
    vim.print(vim.g.some_global_variable)
    --> { key1 = "value", key2 = 400 }
<
To delete a variable, simply set it to `nil`:
>lua
    vim.g.myvar = nil
<
------------------------------------------------------------------------------
See also:
• |lua-vim-variables|

==============================================================================
Options                                                      *lua-guide-options*

There are two complementary ways of setting |options| via Lua.

------------------------------------------------------------------------------
vim.opt

The most convenient way for setting global and local options, e.g., in `init.lua`,
is through `vim.opt` and friends:

• |vim.opt|:        behaves like |:set|
• |vim.opt_global|: behaves like |:setglobal|
• |vim.opt_local|:  behaves like |:setlocal|

For example, the Vimscript commands
>vim
    set smarttab
    set nosmarttab
<
are equivalent to
>lua
    vim.opt.smarttab = true
    vim.opt.smarttab = false
<
In particular, they allow an easy way to working with list-like, map-like, and
set-like options through Lua tables: Instead of
>vim
    set wildignore=*.o,*.a,__pycache__
    set listchars=space:_,tab:>~
    set formatoptions=njt
<
you can use
>lua
    vim.opt.wildignore = { '*.o', '*.a', '__pycache__' }
    vim.opt.listchars = { space = '_', tab = '>~' }
    vim.opt.formatoptions = { n = true, j = true, t = true }
<
These wrappers also come with methods that work similarly to their |:set+=|,
|:set^=| and |:set-=| counterparts in Vimscript:
>lua
    vim.opt.shortmess:append({ I = true })
    vim.opt.wildignore:prepend('*.o')
    vim.opt.whichwrap:remove({ 'b', 's' })
<
The price to pay is that you cannot access the option values directly but must
use |vim.opt:get()|:
>lua
    print(vim.opt.smarttab)
    --> {...} (big table)
    print(vim.opt.smarttab:get())
    --> false
    vim.print(vim.opt.listchars:get())
    --> { space = '_', tab = '>~' }
<
------------------------------------------------------------------------------

Title: Lua: Vim Variables and Options
Summary
This section details how to work with Vim variables and options from Lua. It describes how to access and modify global, buffer, window, and tabpage variables using `vim.g`, `vim.b`, `vim.w`, and `vim.t`, including how to target specific buffers or windows. It also explains how to set Vim options using `vim.opt`, `vim.opt_global`, and `vim.opt_local`, and how to work with list-like and map-like options using Lua tables. It covers appending, prepending, and removing elements from options and retrieving option values using `vim.opt:get()`.