tmux: Terminal Multiplexer

tmux runs your terminal inside a session that lives on the server, not on your laptop. A session holds windows (like tabs), a window holds panes (sections of…

The build (the process that prepares a project's code for use) takes 20 minutes. You start it on the server over SSH, the boarding-house WiFi drops at minute 15, and the build dies with it. tmux (terminal multiplexer, a program that turns one terminal into many) prevents that. tmux runs your terminal inside a session that lives on the server, not on your laptop. You can leave that session and come back later, and every program inside it keeps running. The session is like a rented house: you can go out (detach) and come home (attach), and your things (the running programs) stay where you left them. Inside the house are several rooms (windows), and each room can be divided into sections (panes). On macOS, tmux is not installed (install it with brew install tmux), but screen, an older program that does a similar job, is already there.

Session, window, pane

tmux has three levels: session, window and pane. This is the Lab's session, the same as the exercise's:

session "dev"
├── window 0 "editor"   (3 panes)
│   ├── pane 0
│   ├── pane 1
│   └── pane 2
└── window 1 "server"   (2 panes)
    ├── pane 0
    └── pane 1

The green line at the bottom of the tmux screen is the status bar (a line of information): [dev] 0:editor* 1:server-. It holds the session's name, then each window's number and name. The * marks the window that's open now, and the - marks the window that was open before it.

Prefix: Ctrl+b first, then the key

Every tmux shortcut starts with the prefix: press Ctrl+b, release it, then press the key. Without the prefix, the key is just typed into the shell. The last column of this table is the tmux command that the key runs:

Key What it does Its tmux command
Ctrl+b c make a new window new-window
Ctrl+b n / Ctrl+b p next / previous window next-window / previous-window
Ctrl+b 0 to 9 jump to that window number select-window
Ctrl+b , rename the window rename-window
Ctrl+b % split the pane into left and right split-window -h
Ctrl+b " split the pane into top and bottom split-window -v
Ctrl+b o go to the next pane (or Ctrl+b then an arrow) select-pane
Ctrl+b z zoom the active pane to full screen, press again to go back resize-pane -Z
Ctrl+b x close the pane, after confirming with y kill-pane
Ctrl+b d detach: leave, the session keeps running detach-client
Ctrl+b [ scroll mode, q to leave copy-mode

Be careful with the name: tmux calls % a horizontal split (-h), because the panes sit side by side, even though the line between them is vertical.

Detach and attach: the session stays alive

This is how you make a session called dev on the server:

deploy@web-1:~$ tmux new -s dev

Inside that session, you run npm run build, then press Ctrl+b d to detach. After that you list the sessions, then go into the session again:

[detached (from session dev)]
deploy@web-1:~$ tmux ls
dev: 2 windows (created Thu Oct  8 14:37:02 2026)
deploy@web-1:~$ tmux attach -t dev

When your SSH connection drops, only your connection to tmux is cut. The session, npm run build inside it included, keeps running on the server. Connect with SSH again, then run tmux attach -t dev. Without tmux, your shell is closed when SSH drops, and the build dies from the SIGHUP signal (from the Process Management lesson).

A tmux session as a JavaScript object

In the exercise, you write createTmuxSession(name): a function that keeps a list of windows inside it, then returns an object of methods (functions inside an object). This pattern is called a closure: the data is kept inside the function, and only the methods that the function returns can change it. This is a small example, a counter:

function createCounter() {
  let count = 0;                       // only the method below can change it
  return {
    add() { count = count + 1; return count; },
  };
}

const c = createCounter();
console.log(c.add()); // 1
console.log(c.add()); // 2

count stays stored between the two calls to c.add(), so the results are 1, then 2. With the same pattern, this is what happens in the exercise for the session dev, stored in the variable s:

Call Returns State afterwards
s.addWindow("editor") 0 editor: 1 pane
s.addWindow("server") 1 server: 1 pane
s.splitPane(0, "h") 2 editor: 2 panes
s.splitPane(0, "v") 3 editor: 3 panes
s.splitPane(1, "h") 2 server: 2 panes
s.detach() "detached: dev" no longer attached

⚠️ Keys and habits that cause trouble in a session

TL;DR: tmux runs your terminal inside a session that lives on the server, not on your laptop. A session holds windows (like tabs), a window holds panes (sections of the screen visible at the same time), and a new window starts with 1 pane. Every shortcut starts with the prefix Ctrl+b (press it, release it, then press the key): c new window, n/p switch window, % split left-right, " split top-bottom, o next pane, z zoom, x close the pane, d detach (leave, the session keeps running). tmux new -s dev makes a session, tmux ls lists the sessions, and tmux attach -t dev goes into it again. A dropped SSH connection doesn't stop the session, so a program inside tmux, such as a build, keeps running.

⚡ Make a session

Write:

tmux new -s dev

Meaning: tmux runs your terminal inside a session. This command makes a session called dev, which lives on the server, not on your laptop. Inside the session are windows (like tabs), and each window can be split into panes (sections of the screen).

⚡ The prefix first

Meaning: Every tmux shortcut starts with the prefix: press Ctrl+b, release it, then press the key. c makes a new window, % splits the pane into left-right, " into top-bottom, and d detaches: you leave, but the session keeps running.

⚡ SSH drops, the session stays alive

Guess first: you connect to the server over SSH, then run a build (the process that prepares a project) inside a tmux session. Then the WiFi drops. Does the build die too?

No. Only your connection to tmux is cut. The session, build included, keeps running on the server.

Meaning: Without tmux, your shell closes when SSH drops, and the build dies with it.

⚡ Go back in

Write:

tmux ls
tmux attach -t dev

Meaning: tmux ls lists the sessions still alive, and tmux attach -t dev goes into the session dev again. To leave for a while, use Ctrl+b d, not exit: exit in the last pane ends the session.

What you will learn