Lua Fundamentals

Lua Fundamentals

Variables

local name = "Neovim"
local version = 10

local scopes a variable to the current file or block. Configuration code should essentially always use it — globals leak across every script Neovim runs.

Tables — the only data structure

Lua has one container, the table. It is a list and a dictionary at once:

local list = { "one", "two", "three" }   -- list-like, 1-indexed!
local dict = { name = "kn", uses_vim = true }

print(list[1])     -- "one"  (Lua counts from 1)
print(dict.name)   -- "kn"

Every plugin’s setup({ ... }) call takes a table. Nested config is nested tables — that is all those braces are.

Functions

local function greet(person)
  return "Hello " .. person   -- .. concatenates strings
end

print(greet(name))

require

require("mod") finds mod.lua (or mod/init.lua) on the runtime path, runs it once, caches the result, and returns whatever the file returned. That single mechanism powers all of Neovim’s module system — and the next page uses it to structure your config.

Talking to Neovim

The vim global is the bridge:

vim.opt.number = true                     -- options (like :set)
vim.g.mapleader = " "                     -- globals (like g:)
vim.keymap.set("n", "<leader>e", vim.cmd.Ex)  -- mappings
vim.cmd("colorscheme hackertheme")        -- any ex command