Lua with NeoVim: Modules and Require — local M = {}, return M & require() | Episode 24

5views
00
8:40
T
Taught by Celeste AI - AI Coding Coach
View on YouTube
Description
Real programs don't live in a single file. In this episode we learn the Lua module pattern: creating a module with local M = {}, attaching functions to the module table, exporting with return M, and loading modules with require(). We build a math tools module and use it from a main script — side by side in Neovim splits. Every keystroke is shown on screen with 3-second pauses so you can follow along at your own pace. What You'll Learn: - local M = {} — the standard module pattern - function M.func() — attaching functions to the module table - return M — exporting the module for consumers - require("module") — loading, running, and caching a module - Why M.add not just add — exports vs local functions - tostring() for concatenating booleans Neovim Commands Used: - :vsplit — vertical split for side-by-side editing - Ctrl+w l — switch to right split - Ctrl+w h — switch to left split - :qa — quit all splits Timestamps: 0:00 Intro 0:12 Preview 0:44 Open mathtools.lua 0:48 local M = {} — the module table 1:25 function M.add — attaching to the module 2:21 M.subtract, M.multiply, M.square, M.is_even 4:06 return M — exporting the module 4:16 :vsplit main.lua — side by side editing 4:40 require("mathtools") — loading the module 5:17 Calling module functions — mt.add, mt.subtract 6:09 :!lua main.lua — run and see results 6:18 Review splits — Ctrl+w navigation 6:46 Recap 7:18 Next: String Patterns Practice code: https://github.com/GoCelesteAI/learn-lua-in-neovim Tags lua, lua tutorial, learn lua, lua modules, lua require, lua package.path, lua local M, lua module pattern, lua code organization, lua multiple files, lua exports, lua for beginners, neovim lua, neovim tutorial, learn lua in neovim, lua episode 24, programming tutorial, coding tutorial, beginner programming, lua 2026, neovim 2026, screenkey, code along, lua intermediate, lua project structure
Back to tutorials

Duration

8:40

Published

March 8, 2026

Added to Codegiz

March 15, 2026

Open in YouTube