Skip to content
Notes | Calvin Choy
Go back

Rex Terminal Multiplexer Setup

Received the beta invite for the Rex terminal multiplexer by Superlogical. Sharing my configuration based on my current tmux setup.

For the past few years, I’ve used the tmux multiplexer to manage my workspaces and quickly jump between projects at work. It also lets me easily restore previous sessions when I restart my laptop. This has worked great for me and helps with context switching between projects.

Like Neovim, Rex uses Lua for configuration and looks for a config file at ~/.config/rex/init.lua. Rex’s keybindings are added on top of the default ones.

Below are the core functionalities and keybindings that I transferred from tmux to Rex. AI did most of the mapping from tmux to Rex: I pointed Claude Code to the official documentation and told it to check the rex CLI for available commands.

Helpers

Prefix

Mapping the prefix/leader like tmux to ctrl+a and creating leader helper function to create rex binds based on this prefix

-- init.lua

local prefix = "ctrl+a"

local function leader(key)
  return prefix .. ">" .. key
end

require("actions").setup()
require("keybinds").setup(leader)
require("popups").setup(leader)

Pane current working directory

Pane helper module with current working directory helper used when creating popup overlays and new session based on cwd:

--- panes.lua

local M = {}

-- working directory of the focused pane, like tmux's `#{pane_current_path}`
function M.cwd(ctx)
  -- `pwd` is only set when the shell reports it via OSC 7 (a file:// URL), so fall
  -- back to the foreground process's actual cwd
  local terminal = "com.superlogical.terminal"
  local pwd = rex.block.call(terminal, "pwd", { block_id = ctx.block_id })
  local path = pwd and pwd.pwd
  if path then
    return (path:gsub("^file://[^/]*", ""):gsub("%%(%x%x)", function(h)
      return string.char(tonumber(h, 16))
    end))
  end
  local proc = rex.block.call(terminal, "process", { block_id = ctx.block_id })
  return proc and proc.foreground and proc.foreground.cwd or os.getenv("HOME")
end

return M

New sessions based on pane cwd

Rex actions, created a helper action to create a new session based on the current working directory. As of the beta now this is not possible natively in Rex:

-- actions.lua

local M = {}

local pane = require("pane")

function M.setup()
	-- new session named after (and started in) the focused pane's folder,
	rex.action({
		name = "session_here",
		title = "New Session in Current Folder",
		run = function(ctx)
			local shell = "com.superlogical.terminal.shell"
			local home = os.getenv("HOME")
			local path = pane.cwd(ctx)

			local name = path:match("([^/]+)/*$") or "root"
			if path == home then
				name = "home"
			end

			local res, err = rex.call("session.create", {
				label = name,
				initial_windows = {
					{ layout = rex.layout.block({ flavor = shell, options = { cwd = path } }) },
				},
			})
			if not res then
				error(err)
			end
			rex.client.queue("session.select", { session_id = res.session_id })
		end,
	})
end

return M

With these helpers defined I can easily transfer some of my commonly used tmux key binds to Rex equivalent.

Sessions

-- keybinds.lua

	rex.bind(leader("shift+n"), "session_here") -- calling the customa action above
	rex.bind(leader("shift+a"), "session_here") -- calling the custom action above
	rex.bind(leader("s"), "session.switch")
	rex.bind("ctrl+s", "session.switch")
	rex.bind(leader("shift+4"), "session.rename")

Windows (tabs)

-- keybinds.lua

	rex.bind(leader("c"), "client.tab.new")
	rex.bind(leader(","), "client.tab.rename")

	-- prefix+h / prefix+l previous / next window
	rex.bind(leader("h"), "client.tab.previous")
	rex.bind(leader("l"), "client.tab.next")

	-- prefix+alt+h / alt+l move window left / right
	rex.bind(leader("alt+h"), "client.tab.move.backward")
	rex.bind(leader("alt+l"), "client.tab.move.forward")

Panes

-- keybinds.lua

	-- splits: down
	rex.bind(leader("shift+'"), "pane.split", { direction = "down" })
	rex.bind(leader("-"), "pane.split", { direction = "down" })

	-- splits: right
	rex.bind(leader("shift+5"), "pane.split", { direction = "right" })
	rex.bind(leader("\\"), "pane.split", { direction = "right" })

	-- focus with prefix+arrows
	rex.bind(leader("left"), "pane.focus.left")
	rex.bind(leader("down"), "pane.focus.down")
	rex.bind(leader("up"), "pane.focus.up")
	rex.bind(leader("right"), "pane.focus.right")

	-- kill pane with prefix+x or prefix+ctrl+x
	rex.bind(leader("x"), "pane.close")
	rex.bind(leader("ctrl+x"), "pane.close")

	-- zoom (tmux default prefix+z)
	rex.bind(leader("z"), "pane.zoom")

	-- resize with prefix+alt+arrows (tmux: 5 vertical, 10 horizontal)
	rex.bind(leader("alt+up"), "pane.resize", { direction = "up", amount = 5 })
	rex.bind(leader("alt+down"), "pane.resize", { direction = "down", amount = 5 })
	rex.bind(leader("alt+left"), "pane.resize", { direction = "left", amount = 10 })
	rex.bind(leader("alt+right"), "pane.resize", { direction = "right", amount = 10 })

Popups

--- popups.lua

local pane = require("pane")

local M = {}

local function popup(name, title, cmd)
  rex.action{
    name = name,
    title = title,
    run = function(ctx)
      local res, err = rex.call("session.new_layer", {
        session_id = ctx.session_id,
        bounds = { 0, 0, 1, 1 },
        layout = { block = {
          flavor = "com.superlogical.terminal.shell",
          options = { cwd = pane.cwd(ctx) },
        } },
        focus = true,
      })
      if not res then
        error(err)
      end
      rex.block.call("com.superlogical.terminal", "write", {
        block_id = res.block_ids[1],
        data = cmd .. "; exit\r",
      })
    end,
  }
end

-- `leader` builds a prefixed key sequence, as in init.lua
function M.setup(leader)
  popup("popup_lazygit", "Popup: lazygit", "lazygit")
  popup("popup_yazi", "Popup: yazi", "yazi")
  popup("popup_btop", "Popup: btop", "btop")
  rex.bind(leader("g"), "popup_lazygit")
  rex.bind("shift+cmd+e", "popup_yazi")
  rex.bind(leader("b"), "popup_btop")
end

return M

Follow-up

Rex is still in early beta, and I haven’t tried its main features yet, such as remote servers and the AI integrations. For now, I’ll keep my tmux setup as my daily driver. That said, once I start offloading (AI) tasks to a remote server, I can imagine the seamless integration being worth exploring.



Previous Post
Using the MacOS Built-In Color Picker