Lua Session 01: Fundamentals

Starting from zero. This session covers Lua’s core types, string operations, and control flow - the foundation for Neovim configuration.

Pre-Session State

  • Can open Neovim

  • Know basic programming concepts

  • Understand configuration files concept

Setup

Test Lua in Neovim command mode:

:lua print("Hello from Lua!")

Or create a test file:

nvim /tmp/test.lua

Lesson 1: Basic Types

Concept: Lua has nil, boolean, number, string, function, table.

Exercise 1.1: Variables and nil

-- Variables are global by default
hostname = "kvm-01"
print(hostname)

-- nil means "no value"
print(undefined_var)  -- nil

-- local scoping (preferred)
local ip = "10.50.1.110"
print(ip)

Exercise 1.2: Booleans

local active = true
local stopped = false

-- Only nil and false are falsy
-- 0 and "" are truthy!
if 0 then print("0 is truthy in Lua!") end
if "" then print("empty string is truthy!") end

Exercise 1.3: Numbers

local port = 22
local cpu = 85.5
local hex = 0xFF  -- 255

print(port + 1)
print(cpu / 100)
print(10 % 3)    -- modulo: 1
print(2 ^ 10)    -- power: 1024

Lesson 2: Strings

Concept: Strings use concatenation with .. operator.

Exercise 2.1: String basics

local name = "kvm-01"
local domain = "inside.domusdigitalis.dev"

-- Concatenation with ..
local fqdn = name .. "." .. domain
print(fqdn)

-- Length with #
print(#name)  -- 6

Exercise 2.2: String functions

local hostname = "KVM-01"

print(string.lower(hostname))  -- kvm-01
print(string.upper(hostname))  -- KVM-01
print(string.sub(hostname, 1, 3))  -- KVM (1-indexed!)
print(string.find(hostname, "-"))  -- 4 (position of -)

Exercise 2.3: String formatting

local name = "kvm-01"
local ip = "10.50.1.110"
local port = 22

-- printf-style
print(string.format("Host: %s at %s:%d", name, ip, port))

-- Template pattern
local template = "Connecting to %s..."
print(string.format(template, name))

Lesson 3: Operators

Concept: Lua uses ~= for not-equal (not !=).

Exercise 3.1: Comparison operators

local a = 10
local b = 20

print(a == b)   -- false (equal)
print(a ~= b)   -- true (NOT equal - note the ~)
print(a < b)    -- true
print(a <= b)   -- true

Exercise 3.2: Logical operators

local active = true
local ready = false

print(active and ready)  -- false
print(active or ready)   -- true
print(not active)        -- false

-- Short-circuit for defaults
local port = nil
local actual_port = port or 22
print(actual_port)  -- 22

Exercise 3.3: String concatenation

-- .. for strings (not +)
local greeting = "Hello" .. " " .. "World"
print(greeting)

-- Numbers auto-convert
local msg = "Port: " .. 22
print(msg)

Lesson 4: Control Flow

Concept: if/then/else/end blocks.

Exercise 4.1: If statements

local cpu = 85

if cpu > 90 then
    print("CRITICAL")
elseif cpu > 70 then
    print("WARNING")
else
    print("OK")
end

Exercise 4.2: For loops

-- Numeric for
for i = 1, 5 do
    print(i)
end

-- With step
for i = 10, 1, -2 do
    print(i)  -- 10, 8, 6, 4, 2
end

Exercise 4.3: While loops

local count = 0
while count < 3 do
    print("Count: " .. count)
    count = count + 1
end

-- Repeat-until (runs at least once)
repeat
    print("Running...")
    count = count - 1
until count == 0

Summary: What You Learned

Concept Lua Syntax Note

Local variable

local x = val

Always use local!

Not equal

~=

NOT !=

Concatenation

..

"a" .. "b""ab"

String length

#str

#"hello"5

Default value

x or default

nil or 2222

If block

if…​then…​end

Must have end

For loop

for i=1,n do…​end

Inclusive range

Key Differences from Other Languages

Concept Other Languages Lua

Not equal

!=

~=

String concat

+

..

Array start

0

1

Falsy values

0, "", null, false

nil, false only

Block end

} or indentation

end

Exercises to Complete

  1. [ ] Create variables for hostname, IP, and VLAN

  2. [ ] Write an if/elseif/else for CPU alert levels

  3. [ ] Loop from 1 to 10, print only even numbers

  4. [ ] Build a formatted string with host info

Next Session

Session 02: Tables - Arrays, dictionaries, iteration.

Session Log

Timestamp Notes

Start

<Record when you started>

End

<Record when you finished>