tmux control mode for Emacs, built on Ghostel.
ghostel-tmux connects to a tmux session via control mode (tmux -CC) and
shows each tmux pane as a ghostel buffer: full VT engine, scrollback,
isearch, occur, copy mode and evil integration all work on pane content.
The panes of a tmux window are Emacs window splits, and the tmux windows are
tabs in the tab line.
- tmux 3.0 or later.
- Ghostel 0.65.0 or later, with its native module.
| Command | What it does |
|---|---|
M-x ghostel-tmux-new | Create or attach to a session by name. |
M-x ghostel-tmux-attach | Attach to the session tmux attach would pick: the most recently used one without a client, else the most recently used. Sessions that ghostel-tmux-skip-sessions-regexp matches are passed over. With C-u, pick the session via completing-read. |
M-x ghostel-tmux-detach | Detach the current session. Its pane buffers go, with C-u they stay; see ghostel-tmux-keep-detached-panes. |
M-x ghostel-tmux-capture | One-shot snapshot: dump a single pane’s scrollback into a fresh buffer (no live link). |
M-x ghostel-tmux-next-pane / ghostel-tmux-prev-pane | Cycle through sibling panes, across tmux windows (C-t C-n / C-t C-p). |
M-x ghostel-tmux-switch-pane | completing-read picker over sibling panes (C-t w). |
M-x ghostel-tmux-switch-session | completing-read picker over the server’s other sessions and the sessions this Emacs is attached to on other servers (C-t s). |
M-x ghostel-tmux-restore-split | Split the window’s tmux window again as tmux lays it out (C-t r). |
M-x ghostel-tmux-reset | Escape hatch: reset tmux state when the protocol parser is wedged. |
With a remote default-directory, ghostel-tmux-new, ghostel-tmux-attach and
ghostel-tmux-capture run ghostel-tmux-program on that host through TRAMP. A
tmux server started this way keeps the environment of TRAMP’s shell, such as
PAGER=cat; start the server on the host first to avoid that.
A session that ghostel-tmux-new or ghostel-tmux-attach is attached to already
gets no second client: the selected window shows it. C-t s moves the client
to another session, whose panes take the place of the ones shown; a session
picked there that is attached in this Emacs already is shown instead, and no
client moves. The sessions this Emacs is attached to on other tmux servers are
offered too, as NAME@HOST, and shown the same way.
ghostel-tmux-socket selects a tmux server other than the default one. With a
prefix argument, ghostel-tmux-new and ghostel-tmux-capture prompt for it,
ghostel-tmux-attach with two (C-u C-u), and an empty answer is the default
server.
In a pane buffer of a live session, ghostel-tmux-capture asks that session’s
tmux, also in the takeover. A path is on the host of default-directory, which
also reaches a server whose TMUX_TMPDIR is not set in TRAMP’s shell. The
socket and program for a host can be set with connection-local variables:
(connection-local-set-profile-variables
'my-tmux-on-build
'((ghostel-tmux-socket . "/run/user/1000/tmux-1000/default")))
(connection-local-set-profiles
'(:application tramp :machine "build") 'my-tmux-on-build)Emacs does not wait for the session: its active pane takes the selected window
once tmux reports it. TRAMP sets up a remote shell for every process it starts,
which takes many round trips on a slow link. Its direct async processes start
ssh alone; they work for a single hop and are turned on for a host with:
(connection-local-set-profile-variables
'my-direct-async '((tramp-direct-async-process . t)))
(connection-local-set-profiles
'(:application tramp :machine "build") 'my-direct-async)A TRAMP older than 2.7.1, as in Emacs 29, takes the connection property
direct-async-process instead; see the TRAMP manual, “Improving performance of
asynchronous remote processes”.
(ghostel-tmux-takeover-mode 1)With the mode on, a tmux -CC new or tmux -CC attach typed in a ghostel
shell turns that shell’s buffer into the controller. This works in
native-PTY, Emacs-PTY and TRAMP sessions alike, and for tmux -CC attach
typed inside an ssh session. The first pane shows up in the window the
shell was in, sized to it, and the tab line lists the session’s windows (its
panes with ghostel-tmux-split-windows nil).
While tmux is attached, what is typed in the shell buffer goes to tmux as
commands, so a window switching to it shows its previous buffer instead,
and M-x ghostel starts a new shell.
When tmux leaves control mode (M-x ghostel-tmux-detach, C-t d in a pane, or
anything else that ends the session), every window showing a pane goes
back to the shell buffer.
A tmux client that dies without detaching (kill -9, a crash) leaves control
mode open until the shell prints an escape sequence, which most prompts
do. With a plain prompt such as macOS’s /bin/bash, run
M-x ghostel-tmux-reset and press C-l.
A raw escape sequence in tmux’s output, such as run-shell printing colors
from C-t :, ends control mode as well: tmux is told to leave, the panes
become snapshots, and the shell shows the end of the control-mode stream.
With the mode off, tmux -CC in a ghostel shell shows nothing until tmux
exits, as in any terminal without control-mode support.
shell/ has an optional tmux wrapper for bash, zsh, fish and nushell that
adds -u -CC to the subcommands that attach (tmux new, tmux attach, tmux a,
or bare tmux) and passes everything else through, also tmux new -d, which
has no client to take over, tmux -c, and any tmux in a tmux pane. In bash,
zsh and fish a tmux alias or function defined before sourcing it stays in
place of the wrapper. Source it in ghostel only:
# bash; for zsh use ghostel-tmux.zsh
[[ "${INSIDE_EMACS%%,*}" = 'ghostel' ]] && source /path/to/ghostel-tmux/shell/ghostel-tmux.bashstring match -q 'ghostel*' "$INSIDE_EMACS"; and source /path/to/ghostel-tmux/shell/ghostel-tmux.fish# config.nu; the wrapper checks INSIDE_EMACS itself
source /path/to/ghostel-tmux/shell/ghostel-tmux.nuBypass it with command tmux ... (^tmux ... in nushell), or by setting
GHOSTEL_TMUX_NO_CC before sourcing it (at any time in nushell).
Pane buffers bind C-t (customizable via ghostel-tmux-prefix-key, e.g. to C-b)
as a tmux-style prefix. These translate into tmux control-mode commands sent
over the active session, so they work the same whether you started tmux via
ghostel-tmux-new or by typing tmux -CC in a shell.
| Key sequence | tmux command | Action |
|---|---|---|
C-t " | split-window -v | Split current pane horizontally (new below). |
C-t % | split-window -h | Split current pane vertically (new right). |
C-t c | new-window | Create a new window. |
C-t x | kill-pane | Kill the current pane. |
C-t z | resize-pane -Z | Zoom the current pane, or unzoom (splits). |
C-t & | kill-window | Kill the current window. |
C-t n | next-window | Cycle to the next window. |
C-t p | previous-window | Cycle to the previous window. |
C-t l | last-window | Switch to the window used last. |
C-t o | select-pane | Next pane of the window. |
C-t C-n | - | Next pane in tab order, across windows. |
C-t C-p | - | Previous pane in tab order, across windows. |
C-t w | - | Pick a pane with completing-read. |
C-t s | switch-client -t | Pick a session with completing-read. |
C-t ( | switch-client -p | Switch to the previous session. |
C-t ) | switch-client -n | Switch to the next session. |
C-t L | switch-client -l | Switch to the session used last. |
C-t r | - | Split the window again as tmux lays it out. |
C-t , | rename-window | Prompt and rename the current window. |
C-t $ | rename-session | Prompt and rename the current session. |
C-t 0 … 9 | select-window -t :N | Jump to window N. |
C-t : | (prompt for any cmd) | Run a tmux command and show its output. |
C-t d | - | Detach (ghostel-tmux-detach). |
C-t ? | - | Describe the prefix map. |
C-t C-t | - | Send a literal prefix byte to the program. |
The map is ghostel-tmux-prefix-map; rebind individual keys however you like.
With repeat-mode, C-n and C-p repeat after C-t C-n or C-t C-p.
A tmux window of several panes shows them all at once: the Emacs window
showing one of its panes is split to match tmux’s layout, and tmux lays the
panes out in that window’s size. Windows beside it stay as they are. A
split from Emacs (C-t %, C-t "), from another client or from a command
such as select-layout splits the Emacs window too, and each layout tmux
settles on moves the window edges to its pane sizes.
Selecting a window of the split, also with C-t o, makes its pane tmux’s
active pane, and a pane tmux makes active, as from another client, gets the
selection while a window of the split has it. Switching tmux windows
(C-t n, C-t 1) shows the other window in the place of the split.
Dragging a divider or a mode line between windows of the split, or resizing
one with C-x { and the like, resizes the pane in tmux. Swapping windows of
the split (windmove-swap-states-left and the like) swaps their panes in tmux
(swap-pane), and panes swapped in tmux (C-b {) show in their new places.
C-t z zooms the current pane to the whole split, and shows all panes of its
window again. Deleting all windows of a split but one (C-x 1) zooms its
pane, and deleting some builds the split again. A window of the split can
show another buffer for a while and keeps its place until tmux
changes the window’s layout, and a window split off
one of its windows (C-x 3) gets a place beside the split.
M-x ghostel-tmux-restore-split (C-t r) shows the split as tmux lays it out
again: its windows show their panes, lost windows come back, and a zoomed pane
is unzoomed. Run from a window showing a pane outside the split, it moves the
split there.
tmux has one size per client, for all windows of its session: of two splits of one session shown at once, the one not of the current tmux window shows its panes at the other’s size, and a divider dragged in it snaps back.
With ghostel-tmux-split-windows nil, an Emacs window shows one pane, which
tmux zooms when its window has several.
Pane buffers have a tab line with a tab per tmux window. Click a tab to show
that window in the Emacs window clicked, or where Emacs shows it already, or
hit C-t n / C-t p. A split has one tab line over its top row of windows,
each showing its part, and no tab line further down. C-t C-n / C-t C-p
and C-t w go to a pane of any window.
A tab has the face ghostel-tmux-tab-activity when its window printed
something while no Emacs window showed it, and ghostel-tmux-tab-bell when
its bell rang; both go when you show the window. What a program prints in
the second after its window’s layout changed does not count, as programs
repaint then. A tab also shows the status its programs report (OSC 7501,
OSC 9;4 progress), as ghostel’s mode line does, for the states in
ghostel-status-indicator-states.
ghostel-tmux-tab-name-function returns a tab’s text. It gets a plist with
the window’s :id, :name, :index, :activity, :bell and :status;
to show tabs as tmux’s status line does:
(setq ghostel-tmux-tab-name-function
(lambda (window)
(format "%s:%s%s%s"
(plist-get window :index)
(plist-get window :name)
(cond ((plist-get window :bell) "!")
((plist-get window :activity) "#")
(t ""))
(or (plist-get window :status) ""))))With ghostel-tmux-split-windows nil, the tabs are the session’s panes, without
marks or status, and a click switches panes in the same window.
A pane’s terminal follows the Emacs windows showing it, like any ghostel
buffer. Each new size is sent to tmux with refresh-client -C cols,rows,
so the server lays the panes out for the real client size. For a split, the
size is what the split windows show, with the column or row tmux puts
between panes, and each pane keeps the size tmux gives it. Where Emacs
spends more between two windows than tmux’s separator, such as two borders
in a nested split, the window beside them can have a column or row more
than its pane.
The floating panes of tmux 3.8 (new-pane, break-pane -W) are in no window
layout, so they get no buffer, and a pane that turns floating loses its buffer.
With a floating pane active, Emacs keeps the tiled panes it shows, or shows the
window’s first tiled pane. With ghostel-tmux-split-windows nil, in a window
with several tiled panes it zooms that pane, which takes the selection from the
floating pane and hides it from other clients too.
A current window that holds only floating panes has nothing to show. On attach, on takeover and when the pane shown goes away, Emacs moves the session to a window with a tiled pane. A takeover that starts in a session with no tiled pane at all stays on the hidden shell.
A package can attach sessions, find their panes and talk to tmux through the
functions below. A session is named by its controller, the hidden buffer
that ghostel-tmux-attach and ghostel-tmux-new return; where a function
takes a BUFFER, that is a controller or one of its pane buffers, by default
the current buffer. A takeover’s controller is the shell buffer tmux -CC
was typed in.
A pane buffer is a ghostel buffer. Showing it in a window, by whatever
command, fits its terminal to the window and tells tmux; a pane of a tmux
window of several panes brings their split with it, see Split windows.
ghostel-send-string in it types into the pane.
| Function | What it does |
|---|---|
(ghostel-tmux-attach SESSION SOCKET NO-DISPLAY) | Attach and return the controller, at once. With NO-DISPLAY, no window is to show the session and the echo area does not report the attach. ghostel-tmux-new takes the same arguments. |
(ghostel-tmux-detach KEEP BUFFER) | Detach the session. |
(ghostel-tmux-controllers) | The controllers of the sessions attached in this Emacs. |
(ghostel-tmux-controller BUFFER) | The controller of BUFFER’s session, nil for any other buffer and for a pane kept after a detach. |
(ghostel-tmux-session-name BUFFER), (ghostel-tmux-session-id BUFFER) | The session’s name, and tmux’s id of it, such as "$3", once tmux has reported them. |
(ghostel-tmux-panes BUFFER) | The session’s pane buffers, in tmux’s order. |
(ghostel-tmux-pane-buffer PANE-ID BUFFER) | The buffer of a pane, by tmux’s id of it, such as "%5". |
(ghostel-tmux-active-pane BUFFER) | The buffer of the active pane of the session’s current window. |
(ghostel-tmux-pane-id PANE), (ghostel-tmux-window-id PANE) | tmux’s ids of the pane and of its tmux window, such as "%5" and "@1". |
(ghostel-tmux-window-split WINDOW) | The windows of the split of a tmux window that WINDOW is part of, or nil. |
(ghostel-tmux-select-pane PANE) | Switch to the pane in tmux, and select a window showing it or show it in the selected one. |
(ghostel-tmux-command COMMAND CALLBACK BUFFER) | Send tmux a command line. CALLBACK gets what it printed and tmux’s error message, or nil. |
Panes arrive after the attach has returned, and tmux makes and ends them at any time, so a package follows them through hooks. All but the first run in the controller buffer:
| Hook | Runs |
|---|---|
ghostel-tmux-pane-mode-hook | In a pane buffer that was made, or made live again by an attach; a pane that ends has its buffer killed (kill-buffer-hook). Also in a buffer of ghostel-tmux-capture, which is no pane of a session. |
ghostel-tmux-attach-hook | When tmux has listed the session’s windows: after an attach, also to a session attached already, and after the client switched to another session, for which no detach hook runs. No minibuffer is open then, so a function may change windows. |
ghostel-tmux-detach-hook | When the tmux client ends, also after an attach that failed, while the session’s pane buffers are still there. |
ghostel-tmux-notification-functions | For each notification of tmux’s control mode except pane output, with the line as its argument, such as "%window-renamed @1 name". |
ghostel-tmux-command takes a command that answers with several replies
or none, but detaching is for ghostel-tmux-detach. It signals a
user-error for an empty command, and for a session that is not attached
or whose tmux is leaving. A callback is not called when tmux leaves
first; the detach hook runs then. With tmux’s refresh-client -B, a
package gets %subscription-changed notifications for the formats it
wants to follow.
To show another buffer in place of a split, show it in one window of
ghostel-tmux-window-split and delete the others: the window left then
leaves the split. A buffer shown in a window of the split can lose that
window when tmux changes the panes.
;; Attach in the background, and show the session once its panes are there.
(defvar my-controller (ghostel-tmux-attach "work" nil t))
(add-hook 'ghostel-tmux-attach-hook
(lambda ()
(when-let* (((eq (current-buffer) my-controller))
(pane (ghostel-tmux-active-pane)))
(ghostel-tmux-select-pane pane))))
;; Ask tmux for something.
(ghostel-tmux-command "display-message -p '#{socket_path}'"
(lambda (output failure)
(message "%s" (or failure output)))
my-controller)| Option | Effect |
|---|---|
ghostel-tmux-program | tmux executable. Default "tmux". |
ghostel-tmux-socket | tmux server: a name (-L) or an absolute path (-S). Default nil, the default server. |
ghostel-tmux-skip-sessions-regexp | Sessions whose name matches are not attached unasked: ghostel-tmux-attach picks another one, or none when all match, and the session prompts offer them last. Default nil. |
ghostel-tmux-prefix-key | Prefix key in pane buffers. Default "C-t". |
ghostel-tmux-split-windows | Show the panes of a tmux window as Emacs splits. Default t. |
ghostel-tmux-keep-detached-panes | Keep pane buffers as read-only snapshots (Ghostel:detached) when the session leaves Emacs: ended (default) when it ended, not after a detach, t always, nil never. Attaching to their session again makes them live panes. |
ghostel-tmux-history-lines | Scrollback lines to backfill via capture-pane on attach. Default 10000. |
ghostel-tmux-default-rows / -cols | Initial pane terminal size before the layout-resize fires. Default 40 x 120. |
ghostel-tmux-buffer-name-function | Customize pane buffer naming. Called with (SESSION WINDOW PANE-ID). |
ghostel-tmux-tab-name-function | The text of a tmux window’s tab, see Tab line. |
make all byte-compiles, runs the unit tests and the shell wrapper test,
and lints. make test-native runs the tests that need ghostel’s native
module, built in GHOSTEL_DIR (default ~/.emacs.d/lib/ghostel).
make test-elate drives a real tmux from sandboxed Emacs sessions with
elate. It needs the native module, tmux, and bash, zsh, fish and
nushell at the paths named in the scenarios. Each run has a private tmux
server under /tmp/ghostel-tmux-elate-*.
| Scenario | Covers |
|---|---|
test/elate/takeover.json | tmux -CC in a shell, per host shell and PTY backend, and every way out. |
test/elate/panes.json | Keys, splits, switching, windows, scrollback, a second client, closing, with one pane per Emacs window. |
test/elate/subprocess.json | ghostel-tmux-attach, ghostel-tmux-new and ghostel-tmux-capture, with one pane per Emacs window. |
test/elate/splits.json | Panes as Emacs splits: selection, swaps, restoring, zoom, resizing, switching windows, closing. |
One variant of one scenario:
GHOSTEL_DIR=~/.emacs.d/lib/ghostel \
elate run --emacs "$(command -v emacs)" --variant fish-native test/elate/takeover.json