Rex

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, no goto and no <const>. The tables, functions and strings that this course uses are the same in both.
  • io.popen and os.execute are off. To run a program, you use rex.exec (lesson 7).
  • Rex adds one global table, rex. All its functions are in that table.
  • rex do file.lua runs a script. The script runs on your machine, where the rex command runs. In a Rex pane, it acts on that pane's session unless you say otherwise. If the script returns a value, rex do prints 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.

FunctionWhat 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)

a
b

window 2: rex.layout.horizontal(0.7, a, rex.layout.vertical(0.5, b, c))

a
b
c
My reading of the docs: horizontal puts a on the left and b on the right. You test this in step 4.

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):

OptionWhat it does
cwdThe directory in which the terminal starts. This is the root: of smug.
initial_inputText that Rex types when the terminal starts. This is the commands: of smug.
commandA 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)
end

You 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

  1. Read your session as data

    rex do -e 'return rex.session.view{}' --json

    -e runs 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.

  2. Write the script

    Make the directory ~/.config/rex/scripts and the file code-window.lua in 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 window

    os.getenv("HOME") reads an environment variable. Lua does not expand ~, so you build the path.

  3. 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 --json

    A new tab with the label code opens in the session that you are in. The command prints the table that the script returned. Find window_id in it. Lesson 5 uses that field.

  4. Change the ratio

    Close the tab. Change 0.5 to 0.7 and run the script again. The pane with gitty must 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 horizontal and vertical mean the reverse of my reading, and the reference sheet must change.

  5. Break it

    Change the flavor to com.superlogical.terminal.shel. Run the script, then run echo $?. You get the message from the server and exit status 1.

    Now remove the three lines of the if. Run it again, and run echo $? again. You should get no window, no message and status 0. That is why you check.

    Put the flavor and the if back.

  6. 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.args holds the key=value pairs from the command line. a or b gives a, or b if a is nil. Now the same file works for a different project:

    cd ~/Work/kobun.io
    rex do ~/.config/rex/scripts/code-window.lua root=$PWD
  7. Your turn: the agents window

    Add a second rex.session.new_window call to the script. It makes a window with the label agents and one pane that types claude. A layout can be one block: pass the result of term(...) as the layout. Check the result of this call too.

Check yourself

Check yourself

Answer from memory. Do not scroll up first.

  1. What happens when rex.layout.horizontal(0.3, a, b) runs?

  2. A rex.session call fails. What does your script get?

  3. Which terminal option does the work of the commands: field of smug?

  4. Where does a script run when you start it with rex do?

  5. From lesson 3: you write the key % in a binding as

0 of 5 answered

Primary source

Rex docs: Lua Scripts

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.

Next: lesson 5, one launcher for all projects.