Rex

Open a project from a key

December 04, 2026

At the end, Ctrl+A and then O shows a list of your projects, and Enter opens the one that you pick.

Before you start

From memory

From lessons 3 and 5: you changed a file in ~/.config/rex. Which two commands do you run, in order, and what does each one do? Then: what does check(result, err) in the launcher do, and why does the launcher need it?

A script and an action

A script is a file that you run with rex do. An action is a named Lua function in your config. After a reload you can bind it to a key, find it in the command palette, and run it from the command line (Custom Actions).

ScriptCustom action
Where you write itA .lua file in any directoryinit.lua, or a file that it loads with require
How it startsrex do file.luaA key, the command palette, or rex do name
Who runs the LuaThe rex command, on your machineThe Rex server, which loaded your config
How long it can runTo the end of the file30 seconds
Can it ask you a questionNoYes, with rex.ui.pick and rex.ui.input

You define an action with rex.action{ ... }. Three fields are required: name, title and run. A name has lower-case letters, digits and underscores, and no dot. The names with a dot, such as pane.split, belong to the app.

run is a function with two parameters. ctx says where the action runs: session_id, block_id, client_id and origin. args is a table of the arguments, and it is a table also when there are none.

A list to pick from

rex.ui.pick{ title = ..., items = ... } shows a list in the app and waits (Custom UI). An item is a string, or a table with an id and, if you want, a title, a subtitle and an icon.

The call returns the id of the item that you picked. If you press Escape, it returns nil and the reason "cancelled". Other reasons are "timeout", "no_client", "unsupported", "busy" and "failed".

A prompt works only in the run function of an action, or in a function that run calls directly. It does not work in a function that you bind to a key, in an event handler, or at the top level of the config. So the key must run a named action.

First, the file that you have

  1. Check your old actions.lua

    rex config check takes a path, so it can check a file that is not loaded:

    rex config check ~/.config/rex/actions.lua

    Read the message. Open the file at the line that it names.

From memory

Before you read on: what is wrong at that line? There are two more faults in the same call. Can you find them from what you know of rex.layout.horizontal?

That file tried to do for one project what your launcher now does for all of them. So you replace its content.

Write the action

  1. Keep the old file, then write the new one

    Give the old file a different name, for example actions.old.lua. Then make a new ~/.config/rex/actions.lua:

    -- ~/.config/rex/actions.lua
    -- Custom actions. init.lua loads this file with require("actions").
     
    local projects = require("projects")
    local launcher = require("launcher")
     
    -- A prompt that the person cancelled returns nil and "cancelled". That is not an error.
    -- Other reasons (timeout, no_client, failed) are errors.
    local function answered(value, why)
      if value == nil and why ~= "cancelled" then
        error("prompt failed: " .. tostring(why), 0)
      end
      return value
    end
     
    local function pick_project()
      local items = {}
      for name, project in pairs(projects) do
        items[#items + 1] = {
          id = name,
          title = name,
          subtitle = project.root,
          icon = "folder",
        }
      end
      table.sort(items, function(a, b) return a.id < b.id end)
      return answered(rex.ui.pick{ title = "Open project", items = items })
    end
     
    rex.action{
      name = "open_project",
      title = "Open project",
      description = "Open the Rex session for a project, or make it.",
      category = "Projects",
      keywords = { "smug", "session" },
      args = { project = "string?" },
      run = function(ctx, args)
        local name = args.project or pick_project()
        if name == nil then
          return
        end
        local project = projects[name]
        if project == nil then
          error("no project named " .. name, 0)
        end
        return launcher.open(project)
      end,
    }

    New Lua: pairs(table) goes through all the fields of a table, in no fixed order. That is why the code sorts the items. items[#items + 1] = x adds x at the end of a list. rex.ui.pick returns two values, and answered(...) receives both, because a call that is the last argument passes all its results.

    args = { project = "string?" } says that the action takes one argument, a string, and that it is optional. With the argument, the action opens that project. With no argument, it asks.

  2. Load it and bind it

    In init.lua, add a second line:

    require("keys")
    require("actions")

    At the end of keys.lua, add:

    -- The project picker.
    key("o", "open_project")
  3. Check and reload

    rex config check
    rex config reload
    rex actions

    check must now report 27 bindings and 1 action. rex actions lists the actions that the server offers. open_project must be in the list.

  4. Run it three ways

    From the palette: press Shift+Cmd+P and type "Open project".

    From your key: press Ctrl+A, then O. Pick a project. Press Cmd+K to go to its session.

    From the command line, with no picker:

    rex do open_project project=docudex --json

    The action returns the table from launcher.open, and rex do prints it.

Predict, then test

Add one more project to projects.lua. Run rex config reload. Press Ctrl+A, then O.

Before you look: is the new project in the list? The docs say that the config file runs from top to bottom on each load. They do not say if a file that require loaded is read again. Your test gives the answer. If the project is not in the list, tell Claude, and the next lesson starts with the fix.

Check yourself

Check yourself

Answer from memory. Do not scroll up first.

  1. How long can a custom action run?

  2. You press Escape in the picker. What does rex.ui.pick return?

  3. Where can rex.ui.pick show its list?

  4. Why is the project argument of the action optional?

  5. From lesson 4: who runs the Lua of a script that you start with rex do?

0 of 5 answered

Primary source

Rex docs: Custom Actions

Read the sections on arguments, on ctx and on errors. Then read Custom UI for the full list of fields of an item and the rules for where a prompt can run.

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 7, worktrees.