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
- Session: one named workspace, for example
dev. The session is what stays alive on the server. - Window: one full view inside the session, like a browser tab. Numbered from 0.
- Pane: a section of the screen inside one window. All the panes of a window are visible at the same time. A new window always starts with 1 pane, and every split adds 1 pane.
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
tmux new -s devmakes a session calleddev. The full form istmux new-session -s dev.tmux ls(list-sessions) shows the sessions still alive. When there are no sessions at all, the message isno server running on /tmp/tmux-1000/default.tmux attach -t dev(attach-session) goes into the sessiondevagain, and every window, pane and program inside is still there. When no session has that name, the message iscan't find session: 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 |
addWindowreturns the new window's number. Numbers start at 0, so it's always one less than the number of windows afterwards.splitPanereturns the window's pane count after the split. Its first argument (0or1) is the window's number. The direction"h"or"v"only changes the layout, not the count.- The method
listWindows()returns the list of windows. Its first item,listWindows()[0], holds thenameeditorand thepaneCount3.
⚠️ Keys and habits that cause trouble in a session
- Forgetting the prefix. Pressing
dalone just types the letter d into the shell. Always press Ctrl+b first, release it, then the key. - Typing
exitwhen you meant detach.exitcloses the pane. If it's the last pane of the last window, the session ends too ([exited]) and every program inside dies. To leave for a while, use Ctrl+b d. - Making a new session on every login. The old sessions are still there, and their number keeps growing. A second
tmux new -s devis refused with the messageduplicate session: dev. Check withtmux lsfirst, then go in withtmux attach -t dev. - Running tmux inside tmux. tmux refuses:
sessions should be nested with care, unset $TMUX to force. You're already in a session, so use Ctrl+b c to make a new window.
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.