Rex

One launcher for all projects

December 04, 2026

At the end, rex do open.lua project=v4 does the work of smug start v4, and a new project is a few lines in a table, not a new file.

Before you start

From memory

From lesson 4, with no look back: write the Lua for one pane. It is a shell in ~/Work/ink, and Rex types gitty into it. Then say what a rex.session call returns when it fails.

What your smug files hold

You have 17 files in ~/.config/smug. They differ in four things: the label, the directory, the windows, and the text for each pane. Three of them, cosswork.yml, kobun-io.yml and v4.yml, have the same two windows and differ only in the label and the directory.

That is data. In Lua, data is a table, and the steps that turn one entry into a session are one function. This lesson has three files, all in ~/.config/rex:

FileWhat it holds
projects.luaThe data. One entry for each project.
launcher.luaThe steps. open(project) builds a session from one entry.
open.luaA script of six lines. It reads project= from the command line and calls the launcher.

A Lua file can end with return value. Another file gets that value with require("name"). That is all a module is in Lua.

A session with no windows

In lesson 2, rex new made a session with one window and one shell. Lua can ask the server for a session with no windows, and then add the windows that the entry lists.

rex.call(method, args) calls a server method by its full name (Rex Lua API). session.create takes a label. With no initial_windows, it makes a session with no windows (Server API Methods). After that, rex.session.new_window takes a session_id, so it can add a window to a session other than the one that the script is in.

The docs that I read do not show two things: the fields of the result of session.create, and the fields of the result of session.list. You get them from your own server in step 1.

Do it

  1. Ask the server for the two shapes

    rex api describe session.create
    rex do -e 'return rex.call("session.create", { label = "probe" })' --json
    rex do -e 'return rex.call("session.list")' --json
    rex kill probe

    Write down two things. Which field of the first result holds the ID of the new session? In the second result, where is the list of sessions, and which fields does each session have? The code below expects session_id in the first, and a list in sessions whose items have label and session_id. If your server differs, change the two lines that have the comment CHECK.

  2. Write the data

    Make ~/.config/rex/projects.lua:

    -- ~/.config/rex/projects.lua
    -- One entry for each project. This file replaces the YAML files in ~/.config/smug.
    -- A pane is the text that Rex types into a shell in that pane. "\n" starts a new line.
     
    local home = os.getenv("HOME")
     
    -- cosswork.yml, kobun-io.yml and v4.yml have the same two windows.
    -- A function gives that shape a name.
    local function vp_site(label, root)
      return {
        label = label,
        root = root,
        windows = {
          { name = "code", panes = { "gitty", "vp i\nvp run dev" } },
          { name = "agents", panes = { "claude" } },
        },
      }
    end
     
    return {
      v4 = vp_site("i4o-v4", home .. "/Work/v4.i4o.dev"),
      cosswork = vp_site("cosswork", home .. "/Work/coss.work"),
      ["kobun-io"] = vp_site("kobun-io", home .. "/Work/kobun.io"),
     
      docudex = {
        label = "docudex",
        root = home .. "/Work/docudex",
        windows = {
          { name = "code", panes = { "gitty", "claude" } },
          { name = "agents", panes = { "claude" } },
        },
      },
    }

    A table with name = value fields is like a Go struct or a TypeScript object. A table with values only, such as { "gitty", "claude" }, is a list, and its first item has index 1. A key that is not a plain name needs brackets and quotes: ["kobun-io"].

  3. Write the launcher

    Make ~/.config/rex/launcher.lua:

    -- ~/.config/rex/launcher.lua
    -- Builds a Rex session from one entry in projects.lua.
     
    local M = {}
     
    local SHELL = "com.superlogical.terminal.shell"
     
    -- A Rex call that fails returns nil and a message. It does not raise an error.
    -- check() raises the message, so one failed step stops the launcher.
    local function check(result, err)
      if result == nil then
        error(err or "unknown error", 0)
      end
      return result
    end
     
    -- One pane: a shell in `root`. Rex types `input` into it, like smug's `commands`.
    local function term(root, input)
      local options = { cwd = root }
      if input ~= nil and input ~= "" then
        options.initial_input = input .. "\n"
      end
      return rex.layout.block{ flavor = SHELL, options = options }
    end
     
    -- Panes side by side with equal widths, like smug's even-horizontal.
    -- row({ a, b, c }) gives a one third, then splits the rest between b and c.
    local function row(blocks, from)
      from = from or 1
      local count = #blocks - from + 1
      if count == 1 then
        return blocks[from]
      end
      return rex.layout.horizontal(1 / count, blocks[from], row(blocks, from + 1))
    end
     
    -- Returns the ID of the session with this label, or nil.
    local function find_session(label)
      local result = check(rex.call("session.list"))
      -- CHECK: the list is in result.sessions, or result is the list.
      for _, session in ipairs(result.sessions or result) do
        if session.label == label then
          return session.session_id
        end
      end
      return nil
    end
     
    -- Opens `project`. `opts.root` and `opts.label` replace the project's values.
    function M.open(project, opts)
      opts = opts or {}
      local root = opts.root or project.root
      local label = opts.label or project.label
     
      local existing = find_session(label)
      if existing ~= nil then
        return { session_id = existing, label = label, created = false }
      end
     
      local created = check(rex.call("session.create", { label = label }))
      -- CHECK: the field that holds the new session's ID.
      local session_id = created.session_id
     
      local first
      for _, window in ipairs(project.windows) do
        local blocks = {}
        for i, input in ipairs(window.panes) do
          blocks[i] = term(root, input)
        end
        local made = check(rex.session.new_window{
          session_id = session_id,
          window_label = window.name,
          layout = row(blocks),
          focus = false,
        })
        first = first or made.window_id
      end
     
      check(rex.session.focus_window{ session_id = session_id, window_id = first })
      return { session_id = session_id, label = label, created = true }
    end
     
    return M

    Read it from the bottom. M.open looks for a session with the label. If there is none, it makes one, adds a window for each entry in windows, and puts the first window in front.

    New Lua in this file: ~= is "not equal". #blocks is the length of a list. for _, window in ipairs(list) goes through a list in order, and _ is a name for a value that you do not use. M is a table that the file returns, and function M.open puts a function in it.

  4. Write the script

    Make ~/.config/rex/open.lua:

    -- ~/.config/rex/open.lua
    -- Run it with: rex do ~/.config/rex/open.lua project=v4
     
    -- Tell require where the other files are.
    package.path = os.getenv("HOME") .. "/.config/rex/?.lua;" .. package.path
     
    local projects = require("projects")
    local launcher = require("launcher")
     
    local project = projects[rex.args.project]
    if project == nil then
      error("usage: rex do open.lua project=<name>", 0)
    end
     
    return launcher.open(project)

    package.path is the list of places in which require looks. The ? stands for the module name.

  5. Run it, two times

    rex kill i4o-v4
    rex do ~/.config/rex/open.lua project=v4 --json

    The first line ends the session from lesson 2. If that session is gone, the command says so and changes nothing. The second prints created: true. Press Cmd+K and go to i4o-v4. You must see the two windows of your YAML.

    Run the second line again. It prints created: false and the same session_id. No second session.

  6. Open a project that has no script

    rex do ~/.config/rex/open.lua project=docudex --json

    You wrote no new file for docudex. A few lines of data did it.

  7. Your turn: port one more

    Pick one of ink.yml, scribe.yml or vct.yml. Add an entry for it to projects.lua and open it. If its windows are the same as an entry that you have, call a function, as vp_site does.

How row works

row is the only part with a new idea. It turns a list of panes into nested splits. For three panes, it gives the first pane one third of the width. Then it calls itself for the two panes that are left, and each of them gets one half of what is left.

window 1: row({ a, b, c }) = horizontal(1/3, a, horizontal(1/2, b, c))

a
b
c
Your watchtower.yml has a window with three panes side by side. This is how it is built.

Check yourself

Check yourself

Answer from memory. Do not scroll up first.

  1. What does session.create make when you give it only a label?

  2. row gets three panes. Which share of the width does the first pane get?

  3. Why does open call find_session before it makes a session?

  4. What does require("projects") give you?

  5. From lesson 2: what does rex send add after the text?

0 of 5 answered

Primary source

Rex docs: Rex Lua API reference

Read the section "Calling Rex": rex.session, rex.call and rex.block.call, and the list of session methods. It is the list of things that a script can ask the server to do. The full files of this course, as tested, are in its solutions/ folder. They are the state at the end of lesson 8, so they have more in them than you wrote today.

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 6, open a project from a key.