Command line and persistent sessions
Install moth, open directories, control panes, and detach or reattach running sessions.
Install the moth command
Keep Moth.app in its intended permanent location, normally
/Applications.Launch that copy and choose Moth → Install Command Line Tool….
Choose Install. macOS asks for administrator authentication if the destination requires it.
In your terminal, run:
command -v moth moth --help
The installer creates /usr/local/bin/moth as a symbolic link to the executable inside that app. Updating Moth in the same location automatically updates the command too. If you move or rename the app, run the installer menu action again. Existing files or links belonging to another tool are preserved; the installer reports a conflict instead of replacing them.
If moth is not found, check that /usr/local/bin is on your shell's PATH. You can also invoke the installed app directly:
/Applications/Moth.app/Contents/MacOS/Moth --help
New shells started by Moth also receive these environment variables:
| Variable | Meaning |
|---|---|
MOTH_CLI |
Full path to the app executable. Use "$MOTH_CLI" --help without installing the link. |
MOTH_SOCKET |
The local control endpoint for that Moth instance. The CLI uses it automatically. |
MOTH_PANE_ID |
The ID of the pane that started this shell. Pane commands use it when --pane is omitted. |
Shells created by an older version may lack these variables. Open a new pane or tab to receive them. A terminal outside Moth normally has no MOTH_PANE_ID, so specify a target ID there.
Open a window in a directory
moth .
moth ../another-project
moth ~/Developer/project
moth '/path/with spaces/project'
Each invocation opens a new window in the requested directory. . and other relative paths are resolved from the caller's current directory. The explicit directory takes precedence over the default profile's starting directory and directory-inheritance preference. Moth launches if it is not running.
The argument must be an existing, accessible directory. Files and missing paths produce an error. If a directory has the same name as a CLI command, prefix it with ./, for example moth ./list-panes. Use moth -- ./--project for a path that begins with dashes.
Running moth without arguments shows help. Directory requests and the detached-session commands can launch Moth automatically. Other control commands require the app to be running. Complete any startup or recovery dialogs before retrying a command that reports Moth is still starting.
Find the right pane
Run:
moth list-panes
Successful control commands print JSON containing "ok": true. A pane listing has this shape; the placeholder IDs below stand for the UUIDs returned by your app:
{
"ok": true,
"panes": [
{
"id": "PANE_ID",
"tab": "TAB_ID",
"window": 1,
"tabIndex": 1,
"paneIndex": 1,
"title": "Shell",
"columns": 128,
"rows": 32,
"active": true,
"available": true,
"atPrompt": true,
"broadcasting": false
}
]
}
Use id for --pane and tab for --tab. Window, tab and pane indices are one-based positions, not persistent identifiers. Panes are listed in tab and layout order. More than one window can have an active pane: this field means the active pane of that window's selected tab.
An available pane can accept input. Exited, read-only and restoring panes are unavailable. atPrompt is true when shell integration identifies an input prompt; it is not a generic indication that an arbitrary remote program is ready.
Pane IDs stay with their sessions through detaching, reattachment and recovery. Restoring a saved layout creates fresh shells with new IDs. Use the IDs returned by restore-layout when scripting those new panes.
Errors are written to standard error and return a nonzero exit status. Help is plain text; successful control responses are JSON. Scripts should check the exit status before parsing a response. Every control command accepts --socket PATH to select another local endpoint. Put options before the -- that introduces text or keys.
Send text and control keys
From any terminal, replace PANE_ID with the target from list-panes:
moth send-text --pane PANE_ID -- 'echo ready'
moth send-text --pane PANE_ID --submit -- 'echo ready'
moth send-keys --pane PANE_ID -- Enter
moth send-keys --pane PANE_ID -- C-c
send-text accepts one quoted argument after --. It sends text to that pane without changing focus. --submit adds a Return after the text; it is off by default. Line breaks in text are converted to terminal Return bytes, so multiline text can still affect the receiving program without --submit. Paste uses the destination's text encoding and bracketed-paste setting. Unrepresentable text is rejected before an added Return is sent.
Supported named keys are Enter, Tab, Escape, Backspace, and C-a through C-z. C-c sends Control-C to the target program; it is not a request to close the pane. Arrow keys and arbitrary named key combinations are not currently part of this command.
Inside the pane you want to address, --pane can be omitted:
moth send-text -- 'echo ready'
Explicit CLI sends affect only their target, even if broadcast input is enabled. For a GUI equivalent, choose Shell → Send Text to Pane…, select a destination or all live panes in the current tab, enter the text and optionally enable Send Return after text.
To inspect the terminal display:
moth get-text --pane PANE_ID
The response contains text and truncated. It returns up to the last 64 KiB of terminal text, without terminal styling. This is a view of terminal text, not an unbounded output log or a remote-command completion API.
Control another split from your shell
You can keep a command shell in one split while sending input to another. This example uses jq to read the CLI’s JSON responses. Start in a new tab containing one local shell pane, so the split command creates exactly one other pane.
First, keep the current pane’s ID and create a pane to its right:
controller=$MOTH_PANE_ID
result=$(moth split-pane --pane "$controller" --direction right)
worker=$(printf '%s\n' "$result" | jq -r --arg id "$controller" \
'.panes[] | select(.id != $id) | .id')
moth focus-pane --pane "$controller"
Wait until the new split shows its shell prompt, then send it a command from the controller pane:
moth send-text --pane "$worker" --submit -- 'printf "Ready in the other split\n"; pwd'
The command runs in the other split while your controller pane keeps focus. After the output appears, read it back or send another command:
moth get-text --pane "$worker" | jq -r '.text'
moth send-text --pane "$worker" --submit -- 'git status --short'
Run the Git command in a repository. To interrupt a job in the other split, use moth send-keys --pane "$worker" -- C-c. You can resize it with moth resize-pane --pane "$worker" --width-percent 60, or return keyboard focus to it with moth focus-pane --pane "$worker".
MOTH_PANE_ID identifies the shell that received the environment variable; changing keyboard focus does not change that value. Input delivery is separate from command completion. Scripts can inspect available and atPrompt in list-panes; atPrompt requires shell integration. Open a fresh Moth shell if the environment variables are missing. For automation through macOS applications, see AppleScript.
Broadcast to a tab
Choose Shell → Broadcast Input to Tab, or run:
moth broadcast --tab TAB_ID --on
moth broadcast --tab TAB_ID --off
Tab commands can instead select the tab containing a pane:
moth broadcast --pane PANE_ID --on
Typing, paste and macros from the active pane are sent to every live, writable pane in that tab. The orange broadcast indicator remains visible even if the status bar is normally hidden. Turn the same menu item off to stop.
Broadcasting is off by default, is not restored across app launches, and switches off when fewer than two live panes remain. Terminal-generated replies stay with their own sessions. Mouse reporting is not broadcast. Paste respects each destination's bracketed-paste setting; special keystrokes use the active pane's keyboard protocol. Use targeted input when panes run programs with different keyboard modes.
Split, focus, resize and equalise
moth split-pane --pane PANE_ID --direction right
moth split-pane --pane PANE_ID --direction down
moth focus-pane --pane PANE_ID
moth resize-pane --pane PANE_ID --width-percent 60
moth resize-pane --pane PANE_ID --height-percent 40
moth equal-panes --tab TAB_ID --axis width
moth equal-panes --tab TAB_ID --axis height
moth equal-panes --tab TAB_ID --axis both
Splitting starts a new shell and preserves the original pane's process and history. A tab supports up to nine panes, subject to available space and minimum pane sizes. focus-pane brings the requested window, tab and pane forward.
Resize percentages are between 5 and 95. They refer to the pane's share of the nearest split on that axis, not a percentage of the entire window. In a nested layout, this can resize the group containing the pane. If there is no split on the requested axis, the command returns an error. Minimum sizes can limit the result.
The command is equal-panes. width balances columns, height balances rows, and both applies both axes. Omitting --axis selects both. It preserves the split tree and accounts for nested groups; cell rounding and minimum sizes may prevent exact equality. Resizing or equalising a maximised pane first restores its split layout.
Detach and reattach a session
A session is a pane's shell, PTY and running jobs. Detaching removes the pane while keeping those processes alive. It does not pause execution: jobs can continue to use CPU, produce output and maintain remote connections.
Before creating a session you want to detach, enable Settings → Advanced → Keep new sessions available for detaching and recovery. The corresponding configuration is:
[advanced]
sessionRecovery = true
This starts new sessions through the persistent helper. It cannot retrofit persistence into a session created while the setting was disabled.
In the GUI:
- Focus the pane and choose Shell → Detach Session.
- Later, open Shell → Detached Sessions, choose the session, then Reattach in New Window.
- To terminate a detached shell and its jobs, choose its End Session… action and confirm.
The detached list shows the pane's title and a short ID. The CLI can assign a more specific name:
moth detach-pane --pane PANE_ID --name 'background worker'
moth list-sessions
moth attach-session --session SESSION_ID
moth end-session --session SESSION_ID
When run inside the session itself, moth detach-pane --name 'background worker' uses MOTH_PANE_ID. The session value returned by detach-pane is the ID to use for reattachment. It is also the original pane's ID. list-sessions lists explicitly detached sessions, with id, name and detachedAt (a Unix timestamp).
Reattachment connects to the original shell and replays recorded terminal output. It does not rerun commands. The session leaves the detached list while attached; detach it again before quitting if it should remain independent of the app. An explicitly detached session is not automatically reopened by crash recovery.
| Action | Effect on shells and jobs |
|---|---|
| Detach Session | Keeps this session running and adds it to Detached Sessions. |
| Close a pane | Ends its attached session, with close confirmation when configured. |
| Ordinary Quit | Ends attached sessions; explicitly detached sessions keep running. |
| Quit and Keep Sessions | Keeps attached sessions for the recovery workflow; detached sessions stay in their own list. |
| App crash | Helper-backed attached sessions are recoverable; explicitly detached sessions remain detached. |
| Mac restart | Ends the processes. Reattachment cannot recover them. |
End Session / end-session |
Terminates the selected detached shell and its jobs and removes its entry. |
Replay history is bounded, so very old output can be missing after a session produces enough output. If a helper has ended or the Mac restarted, a detached entry may remain but reattachment will fail. Use End Session… or end-session to remove that unavailable entry.
Run commands in a saved layout
Save a window arrangement using Window → Save Window Layout…. List and restore it from the CLI:
moth list-layouts
moth restore-layout --name 'Work'
Restoring a layout creates fresh shells. The response contains the new pane IDs in tab and layout order. Use those IDs with send-text to start a different command in each pane:
moth send-text --pane PANE_ID --submit -- 'pwd'
Scripts should check each command's exit status and use the returned IDs. list-panes reports available and atPrompt; shell integration identifies local input prompts. Applications running inside a pane may need their own readiness checks.
Macros replay text or recorded keys in the active pane. CLI commands can address individual panes. Detached sessions preserve existing shells and jobs; restoring a saved layout starts new ones.
Troubleshooting
| Symptom | What to check |
|---|---|
moth: command not found |
Install the tool from the intended app and include /usr/local/bin in PATH. |
| The link broke after moving the app | Run Install Command Line Tool… again from the app's new location. |
| A command needs a pane or tab ID | Run list-panes and pass the UUID; environment defaults are normally available only inside new Moth shells. |
| A pane ID stopped working after layout restoration | Fresh layouts create new sessions. Use the IDs in the restore response. |
| Cannot connect to Moth | Launch the app for ordinary control commands; check any MOTH_SOCKET or --socket override and finish startup dialogs. |
| Detach Session is disabled | Wait for restoration to finish, or enable session persistence and create a new session. Exited or direct sessions cannot be detached. |
| Reattachment is unavailable | The helper may have ended or the Mac restarted. Remove the stale detached entry using End Session. |
| A resize fails or stops short | Check that a split exists on that axis and that minimum pane sizes permit the request. |
| A remote job already exists | Reattach it instead of launching the startup script again. |
The control interface uses a local socket restricted to your user account. Requests are bounded to 1 MiB of JSON; use it for commands and interaction rather than file transfer.