Learn Lua in Neovim: Table References vs Copies — The Copy Trap, Shallow & Deep Copy | Episode 22
10views
0014:23
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
Assigning a table to a new variable doesn't copy it — both names point to the same data. In this episode we uncover the copy
trap, show why changes through one variable affect the other, build a shallow_copy function, and then build a recursive
deep_copy for nested tables.
Every keystroke is shown on screen with 3-second pauses so you can follow along at your own pace.
What You'll Learn:
- Tables are references — assignment shares, not copies
- The copy trap — modifying one variable changes both
- shallow_copy(t) — loop and copy top-level keys
- Shallow copy limitation — nested tables are still shared
- deep_copy(t) — recursive function that copies all levels
- When to use shallow vs deep copy
Neovim Commands Used:
- * — search the word under the cursor
- n — jump to next search match
- yy — yank (copy) the current line
- p — paste below
- :s/old/new/g — substitute on the current line
Timestamps:
0:00 Intro
0:12 Preview
0:44 Open file
1:07 The Reference Trap — local b = a
2:07 Run — both a and b are 99
3:11 Shallow Copy function
4:42 Test shallow copy — c and d independent
5:38 Run shallow copy
6:04 Shallow Copy Trap — nested tables shared
7:09 Run — e.scores changed too!
8:16 Deep Copy — recursive solution
10:16 Test deep copy with yy/p/:s
11:58 Run — fully independent
12:29 Review — * search variable, n next match
12:47 Recap
13:19 Next: The Math and String Libraries
Practice code: https://github.com/GoCelesteAI/learn-lua-in-neovim
This is Episode 22 of the Learn Lua in Neovim series. New episodes cover libraries, metatables, OOP, and more — all coded
inside Neovim.
Taught by CelesteAI. Like & subscribe for more tutorials!
#lua #neovim #programming #tutorial #beginners #learnlua #luaprogramming #neovimtutorial #luareferences #luacopy #coding
Tags
lua, lua tutorial, learn lua, lua table references, lua copy trap, lua shallow copy, lua deep copy, lua table assignment, lua
reference vs copy, lua recursive copy, lua clone table, lua tables, lua for beginners, neovim lua, neovim tutorial, learn lua
in neovim, lua episode 22, programming tutorial, coding tutorial, beginner programming, lua 2026, neovim 2026, screenkey, code
along, lua basics, lua data structures
Tags
lualua tutoriallearn lualua table referenceslua copy traplua shallow copylua deep copylua table assignmentlua reference vs copylua recursive copylua clone tablelua tableslua for beginnersneovim luaneovim tutorial