Home Explore Blog CI



neovim

17th chunk of `runtime/doc/lua.txt`
475c2fb7743e2e5785864dfdc336abd5838282c8ca84c7fe0000000100000fa1
  represented directly as a Lua number. Empty lists and dictionaries both
    are represented by an empty table.

    Note: |v:null| values as part of the return value is represented as
    |vim.NIL| special value

    Note: vim.fn keys are generated lazily, thus `pairs(vim.fn)` only
    enumerates functions that were called at least once.

    Note: The majority of functions cannot run in |api-fast| callbacks with some
    undocumented exceptions which are allowed.

                                                           *lua-vim-variables*
The Vim editor global dictionaries |g:| |w:| |b:| |t:| |v:| can be accessed
from Lua conveniently and idiomatically by referencing the `vim.*` Lua tables
described below. In this way you can easily read and modify global Vimscript
variables from Lua.

Example: >lua

    vim.g.foo = 5     -- Set the g:foo Vimscript variable.
    print(vim.g.foo)  -- Get and print the g:foo Vimscript variable.
    vim.g.foo = nil   -- Delete (:unlet) the Vimscript variable.
    vim.b[2].foo = 6  -- Set b:foo for buffer 2
<

Note that setting dictionary fields directly will not write them back into
Nvim. This is because the index into the namespace simply returns a copy.
Instead the whole dictionary must be written as one. This can be achieved by
creating a short-lived temporary.

Example: >lua

    vim.g.my_dict.field1 = 'value'  -- Does not work

    local my_dict = vim.g.my_dict   --
    my_dict.field1 = 'value'        -- Instead do
    vim.g.my_dict = my_dict         --

vim.g                                                                  *vim.g*
    Global (|g:|) editor variables.
    Key with no value returns `nil`.

vim.b                                                                  *vim.b*
    Buffer-scoped (|b:|) variables for the current buffer.
    Invalid or unset key returns `nil`. Can be indexed with
    an integer to access variables for a specific buffer.

vim.w                                                                  *vim.w*
    Window-scoped (|w:|) variables for the current window.
    Invalid or unset key returns `nil`. Can be indexed with
    an integer to access variables for a specific window.

vim.t                                                                  *vim.t*
    Tabpage-scoped (|t:|) variables for the current tabpage.
    Invalid or unset key returns `nil`. Can be indexed with
    an integer to access variables for a specific tabpage.

vim.v                                                                  *vim.v*
    |v:| variables.
    Invalid or unset key returns `nil`.


                                                                 *lua-options*
                                                             *lua-vim-options*
                                                                 *lua-vim-set*
                                                            *lua-vim-setlocal*

Vim options can be accessed through |vim.o|, which behaves like Vimscript
|:set|.

    Examples: ~

    To set a boolean toggle:
        Vimscript: `set number`
        Lua:       `vim.o.number = true`

    To set a string value:
        Vimscript: `set wildignore=*.o,*.a,__pycache__`
        Lua:       `vim.o.wildignore = '*.o,*.a,__pycache__'`

Similarly, there is |vim.bo| and |vim.wo| for setting buffer-scoped and
window-scoped options. Note that this must NOT be confused with
|local-options| and |:setlocal|. There is also |vim.go| that only accesses the
global value of a |global-local| option, see |:setglobal|.


                                                               *vim.opt_local*
                                                              *vim.opt_global*
                                                                     *vim.opt*


A special interface |vim.opt| exists for conveniently interacting with list-
and map-style options from Lua: It allows accessing them as Lua tables and
offers object-oriented method for adding and removing entries.

    Examples: ~

Title: Accessing Vim Variables and Options from Lua
Summary
This section explains how to access and modify Vimscript variables (global, buffer, window, tabpage, and `v:` variables) from Lua using the `vim.*` tables (e.g., `vim.g`, `vim.b`, `vim.w`, `vim.t`, `vim.v`). It highlights that modifying dictionary fields directly doesn't write back to Nvim and provides an example on how to solve it. It also details how to access and set Vim options (global, buffer-scoped, and window-scoped) using `vim.o`, `vim.bo`, and `vim.wo`, respectively, and introduces the `vim.opt` interface for list- and map-style options.