Your first Lua script
December 04, 2026
At the end, one Lua file makes the code window of your v4 session: two panes side by side, in the right directory, with your programs started.
Before you start
From memory
From lessons 1 and 3: Rex has two words for a tab and two words for a pane. Write the four words and say where each pair is used. Then write the command that loads a config file and changes nothing.
Lua in Rex
Four facts, all from the Rex Lua API reference and the Lua Scripts page.
- Rex runs Lua 5.1 with the standard library. Your Lua course uses 5.5. In Rex there is no
//operator, nogotoand no<const>. The tables, functions and strings that this course uses are the same in both. io.popenandos.executeare off. To run a program, you userex.exec(lesson 7).- Rex adds one global table,
rex. All its functions are in that table. rex do file.luaruns a script. The script runs on your machine, where therexcommand runs. In a Rex pane, it acts on that pane's session unless you say otherwise. If the script returns a value,rex doprints it as JSON.
Three ideas
A layout is a table
Three functions build a layout. They call nothing. They only return a table that says what you want.
| Function | What it describes |
|---|---|
rex.layout.block{ flavor = ..., options = ... } | One block. The flavor com.superlogical.terminal.shell is a terminal that runs a shell. |
rex.layout.horizontal(ratio, a, b) | Two layouts, split horizontally. ratio is the share of a, from 0 to 1. |
rex.layout.vertical(ratio, a, b) | Two layouts, split vertically. |
a and b can be blocks or more splits, so you can nest them.
window 1: rex.layout.horizontal(0.5, a, b)
window 2: rex.layout.horizontal(0.7, a, rex.layout.vertical(0.5, b, c))
In Lua, f{ ... } is a call with one table as its argument. It is the same as f({ ... }). Rex uses this form for each call that takes named fields.
initial_input types for you
The options of a terminal block have these fields (Rex Lua API, layouts):
| Option | What it does |
|---|---|
cwd | The directory in which the terminal starts. This is the root: of smug. |
initial_input | Text that Rex types when the terminal starts. This is the commands: of smug. |
command | A list: a program and its arguments. The terminal runs it in place of a shell. |
exit | { on_completion = false } keeps the terminal open after the program exits. |
initial_input and command are the two ways from lesson 2. initial_input is rex send. command is the command after --.
A call returns nil and a message when it fails
rex.session.new_window{ ... } asks the server for a new window. All calls of this kind take one table and return one table. When a call fails, it does not raise an error. It returns nil and a message.
local window, err = rex.session.new_window{ layout = layout }
if window == nil then
error(err, 0)
endYou know this shape from Go: window, err := ... and if err != nil. The difference is that Lua does not make you take the second value. If you do not check, the script goes on with nil. error(err, 0) stops the script with the message. The 0 tells Lua not to add a file name and line number in front.
Do it
Read your session as data
rex do -e 'return rex.session.view{}' --json-eruns one line of Lua.rex.session.view{}returns the windows, layouts and blocks of the session. Find the label of your window in the JSON. This is the data that the layout functions describe.Write the script
Make the directory
~/.config/rex/scriptsand the filecode-window.luain it:-- ~/.config/rex/scripts/code-window.lua -- The "code" window of v4.yml. local root = os.getenv("HOME") .. "/Work/v4.i4o.dev" -- One pane: a shell in root. Rex types `input` into it. local function term(input) return rex.layout.block{ flavor = "com.superlogical.terminal.shell", options = { cwd = root, initial_input = input }, } end local window, err = rex.session.new_window{ window_label = "code", layout = rex.layout.horizontal(0.5, term("gitty\n"), term("vp i\nvp run dev\n")), } if window == nil then error(err, 0) end return windowos.getenv("HOME")reads an environment variable. Lua does not expand~, so you build the path.Predict, then run
Before you run it, write down two answers. Are the two panes side by side, or one above the other? Which program is in the first pane?
rex do ~/.config/rex/scripts/code-window.lua --jsonA new tab with the label
codeopens in the session that you are in. The command prints the table that the script returned. Findwindow_idin it. Lesson 5 uses that field.Change the ratio
Close the tab. Change
0.5to0.7and run the script again. The pane withgittymust be the wide one, because the ratio is the share of the first layout.If the panes were one above the other in step 3, tell Claude. Then
horizontalandverticalmean the reverse of my reading, and the reference sheet must change.Break it
Change the flavor to
com.superlogical.terminal.shel. Run the script, then runecho $?. You get the message from the server and exit status 1.Now remove the three lines of the
if. Run it again, and runecho $?again. You should get no window, no message and status 0. That is why you check.Put the flavor and the
ifback.Take the directory as an argument
Change the line that sets
root:local root = rex.args.root or (os.getenv("HOME") .. "/Work/v4.i4o.dev")rex.argsholds thekey=valuepairs from the command line.a or bgivesa, orbifaisnil. Now the same file works for a different project:cd ~/Work/kobun.io rex do ~/.config/rex/scripts/code-window.lua root=$PWDYour turn: the agents window
Add a second
rex.session.new_windowcall to the script. It makes a window with the labelagentsand one pane that typesclaude. A layout can be one block: pass the result ofterm(...)as thelayout. Check the result of this call too.
Check yourself
Check yourself
Answer from memory. Do not scroll up first.
What happens when rex.layout.horizontal(0.3, a, b) runs?
A rex.session call fails. What does your script get?
Which terminal option does the work of the commands: field of smug?
Where does a script run when you start it with rex do?
From lesson 3: you write the key % in a binding as
0 of 5 answered
Primary source
Read as far as "Layouts", and then the example "Run Tests in a Split". It uses the same three ideas with rex.session.new_split. The calls and options of this lesson are in the Lua sheet.
Stuck, or curious?
Ask Claude. Paste the output or the error and say what you expected. A follow-up question is part of the lesson, and it tells Claude what to teach next.