zterm

Building extensions

A zterm extension is a program or a web page that drives zterm from outside. It can open panes, show its own page in a pane, follow what happens in the terminal, and put itself in the + menu and the command palette. It talks to zterm through the same documented surfaces a script uses, so it needs no build of zterm and no change to it.

zterm has no in-process plugins. Nothing you write is loaded into the zterm process, and no plugin interface is published. The crate's internal extension seam is for zterm's own subsystems, and it changes without notice.

What an extension can use

  • A page in a pane. --web <dir> shows a folder of your HTML, CSS and JavaScript in a pane, themed to match the terminal. --url <address> shows a page that a server on this machine already serves. Showing a page in a pane has every rule.
  • The window.zterm bridge. A --web page gets zterm.call to send any control request, zterm.subscribe to hear the terminal's events, zterm.context to know which pane it is in, and zterm.setTitle to name its pane. A --url page has no bridge.
  • The control socket. zterm perform <action> from a shell or any program, or a JSON line on the socket. Control actions lists every action.
  • Profiles. A [[profiles]] entry in the settings file puts a program or a page in the + menu and the command palette.
  • Status and notifications. A program in a pane reports that it is working, waiting or done with OSC 7501, and asks for a desktop banner with OSC 9 or OSC 777. See Reporting a program's status and Notifications.

A minimal extension

This one is a page that lists the panes in the terminal, marks the focused one, and focuses a pane when you click it. It is one file.

Save it as ~/.local/share/zterm-panes/index.html:

<!doctype html>
<meta charset="utf-8">
<title>Panes</title>
<style>
  body { margin: 12px; background: var(--zt-bg); color: var(--zt-fg);
         font: var(--zt-font-mono-size) var(--zt-font-mono); }
  li { cursor: pointer; }
  li.here { color: var(--zt-ansi-2); }
</style>
<ul id="panes"></ul>
<script>
const list = document.getElementById("panes");

async function draw() {
  const answer = await zterm.call("list", {});
  if (answer.result !== "tree") {
    list.textContent = answer.message || answer.result;
    return;
  }
  list.replaceChildren(...answer.panes.map(pane => {
    const row = document.createElement("li");
    row.textContent = pane.name || pane.title || pane.cwd || pane.pane_id;
    if (pane.focused) row.className = "here";
    row.onclick = () => zterm.call("focus", { target: pane.uri });
    return row;
  }));
}

zterm.subscribe(["pane.*", "focus.*"], draw);
draw();
</script>

Open it beside the pane you are in:

zterm perform split_right --web ~/.local/share/zterm-panes --ratio 0.3

What each part does:

  • The --zt-* variables are the terminal's colours and font, set before the page's scripts run and set again when the theme changes.
  • zterm.call("list", {}) sends the socket's list request and resolves with the same answer zterm ls prints: {"result":"tree","panes":[...]}. A refusal comes back as a value with a result of its own, so the page checks it.
  • zterm.subscribe calls draw again whenever a pane opens, closes, is renamed or moves, and whenever focus changes.
  • zterm.call("focus", {target}) raises the clicked pane.
  • The <title> names the pane, so its tab reads "Panes".

To see what the page logged or threw, and what it looks like, from a shell:

zterm perform read_console --target <pane>
zterm perform screenshot_pane panes.png --target <pane>
zterm perform reload_pane --target <pane>     # after you edit the file, or if it froze

To reload the page while you look at it, press Cmd+R on macOS or Ctrl+R on Linux and Windows with the web pane focused, or pick Reload Page from the command palette's Pane group. Both work only in a web pane: in a terminal pane Ctrl+R goes to the shell as usual. You can move the key under [keybindings] in settings, as reload_pane.

A web pane always shows a title bar, even alone in its tab. At its right end are four buttons: Back, Forward, Reload, and Open in Browser. Back and Forward are dimmed until the page has history that way. Open in Browser opens the page in your default browser, and is dimmed for a page served from an extension's folder, which only zterm can show. A page from another machine shows that machine's name before its title. zterm draws the title bar itself, so a page cannot draw over it or hide the buttons. The command palette's Pane group has Go Back, Go Forward and Open in Browser too. They have no default keys; bind them under [keybindings] as go_back, go_forward and open_in_browser. With pane.title_bar = "never" no pane shows a title bar, a web pane included.

Putting it in the menu

A profile makes the extension one click away. Writing [[profiles]] replaces the shells zterm finds for you, so list your shell too, and name it as default_profile. A new window's first tab and the + button open the profile it names. Without it, a new window opens your OS's default shell and + opens the first profile in the file. Add this to the settings file, above its first [section] line, because default_profile is a top-level key and a line below a section header belongs to that section:

default_profile = "zsh"

[[profiles]]
name = "zsh"
program = "zsh"

[[profiles]]
name = "panes"
kind = "web"
dir = "/Users/you/.local/share/zterm-panes"

Give dir as a full path. The + menu and the command palette now list "panes", and zterm perform open_profile panes opens it. A profile can run a program instead: kind = "command" with program and args runs exactly that, which suits an extension that is a terminal program or a launcher script. Settings has every profile key.

Shipping one

An extension is ordinary files, so it ships however you like: a git repository, a package, a folder to copy. A common shape is a small launcher on PATH that starts a local server if the extension needs one, then opens its page with zterm perform split_right --url http://127.0.0.1:<port>/. A page served that way has no theme variables and no window.zterm, so a launcher that needs the bridge serves its files with --web instead.

A program that runs inside a zterm pane finds the terminal on its own: zterm perform reaches the terminal it runs in, and zterm puts the pane's id in ZESTFUL_TERM_PANE_ID. A perform with no --target acts on the pane that has the focus, so a launcher that means its own pane passes --target "$ZESTFUL_TERM_PANE_ID".

Limits

  • Permissions. A page's calls are made as its pane, and a program's as the pane it runs in. Listing, locating and focusing work for any caller, and opening panes works from any pane. Typing into another pane or reading its screen depends on the pane_access_control setting. For a caller in no pane, the person is asked first. See Permissions.
  • Scripts. A --web page runs only the scripts it ships. A script from another origin is refused, and a link off the page opens in the system browser.
  • Addresses. --url accepts http and https on localhost, 127.0.0.1 and [::1], and https on a host listed in [web] remote in settings. A listed site's page keeps to the listed hosts: a link elsewhere opens in the system browser, while frames inside the page load where they are. Linux shows only pages on this machine for now.
  • Size. A bridge request or answer is at most 65535 bytes. Read a large tree with list_page.
  • Platforms. A page needs a web view: WebKit on macOS, WebView2 on Windows, and WebKitGTK under X11 on Linux. A native Wayland window, and Windows without the WebView2 runtime, refuse to open one. zterm serve has no window and refuses pages.
  • No in-process code. An extension cannot add an action of its own, change how zterm draws a terminal pane, or run inside zterm.