Back to Explore
GuideVibeFix editorialUpdated Nov 25, 2026

Stop Opening a Pile of Terminal Windows: tmux Sessions, Windows, and Panes

tmux 3.4, zero packages: 1 session, 2 windows, 3 panes; the logs window prints 1 to 5, editor panes report ready. Detach with Ctrl-b d, reattach with tmux attach — the session outlives the connection. Mouse, prefix key and history limit in ~/.tmux.conf included.

Real photograph of a vintage desk setup with a CRT monitor showing code (illustrative photo)

Every programmer has hit this scene: a long task is running in a terminal, the SSH connection drops, and the task dies with it. Or the screen has to show logs, tests, and code at the same time, and switching windows gets so confusing you no longer know which window is doing what. tmux exists for exactly this: it is a terminal multiplexer that lets one terminal connection run many sessions, windows, and panes — and when you disconnect, the tasks keep running on the server, waiting for you to reattach.

This guide has a concrete goal: after installing tmux, build one session from scratch, open two windows, split three panes, learn to detach and reattach, learn to pull pane contents out with capture-pane, and finish with a ~/.tmux.conf that feels right. Every command was really run on this machine: tmux 3.4, zero third-party packages. The demo script and its transcripts are in the draft folder, and every number in this article matches them.

Check the version first. Type tmux -V, and if you see tmux 3.4, it is installed. tmux has three layers, largest to smallest: session, window, pane. A session is a group of windows, usually one task or one project; a window is one screen inside a session, like a browser tab; a pane is a slice of a window, like splitting one screen into pieces. The demo script builds exactly these three layers: 1 session, 2 windows, 3 panes.

The command to create a session is tmux new-session -d -s vibe253 -n editor. The -d flag creates it in the background without attaching right away; -s names the session vibe253; -n names the first window editor. Names matter: once sessions pile up, names are how you tell them apart. After creating it, run tmux list-sessions and the list should contain vibe253. That is what Section 1 of the demo does: create session vibe253 with window editor.

Splitting a window into panes uses split-window. In the demo, tmux split-window -h -t vibe253:editor splits the editor window into left and right panes. The -h flag splits left-right, -v splits top-bottom. After the split, the editor window holds 2 panes. Pane numbers start at 0: the left one is editor.0, the right one editor.1. The address format is "session:window.pane," as in vibe253:editor.0. Memorize this addressing scheme — every later command uses it to name its target.

Opening another window uses new-window. The demo runs tmux new-window -t vibe253 -n logs, creating a window called logs. The session vibe253 now has 2 windows, editor and logs. The editor window has 2 panes and logs has 1, totaling 3 panes. Section 3 of the demo counts them: sessions 1, windows in vibe253 2, panes in vibe253 3. Counting panes has a trap: tmux list-panes -t vibe253 counts only the current window's panes, so you must count per window and add them up — the demo tripped on this the first time, counting 1, and only got 3 after the fix.

What runs inside each pane? The demo gives every pane a command that announces itself. Pane editor.0 runs printf "pane-A: editor ready\n", and the logs window runs seq 1 5. After printing, each command sleeps for 120 seconds so the pane stays alive with its output on screen. Section 2 of the demo uses capture-pane to pull out each pane's contents: pane editor.0 shows "pane-A: editor ready," pane editor.1 shows "pane-B: shell ready," and pane logs.0 shows "1 2 3 4 5." capture-pane is one of tmux's most practical commands: pane contents can be grabbed and read at any time without staring at the screen.

Detaching and reattaching is tmux's signature skill. Once attached to a session, press the prefix key Ctrl-b and then d to detach: the session keeps running in the background while the terminal returns to an ordinary shell. To come back, type tmux attach -t vibe253, and the screen is exactly as you left it. The same holds when SSH drops: the connection breaks, the session keeps running on the server, you reconnect, attach, and the task never stopped. That is the fundamental difference from an ordinary terminal: in a plain terminal, tasks follow the connection; in tmux, tasks follow the session.

The prefix key deserves its own paragraph. Nearly every tmux shortcut starts with the prefix, Ctrl-b by default, followed by another key. Ctrl-b d detaches, Ctrl-b c creates a window, Ctrl-b % splits a pane left-right, Ctrl-b " splits top-bottom, Ctrl-b plus an arrow key moves between panes, Ctrl-b plus a number jumps to that window. It feels awkward at first — every shortcut needs Ctrl-b first. But once it becomes habit, the benefit shows: shortcuts never collide with shell shortcuts, so Ctrl-c still interrupts a program while Ctrl-b c creates a window.

A few everyday operations for windows and panes: tmux rename-window -t vibe253:0 newname renames a window; tmux kill-window -t vibe253:logs kills the logs window; tmux kill-pane -t vibe253:editor.1 kills the right pane of the editor window. Killing a session uses tmux kill-session -t vibe253, which is how Section 4 of the demo ends: kill session vibe253, leaving 0 sessions. Think before killing: once a session is gone, everything inside it stops, and there is no recycle bin.

Pane sizes can be adjusted. Inside a session, press Ctrl-b then Alt plus an arrow key, or use tmux resize-pane -D 5 to grow downward by 5 cells (-U, -L, -R for the other directions). Even more useful: Ctrl-b z zooms the current pane to full screen, and pressing it again restores the layout. That is most handy when reading logs: the log pane normally takes half the screen, one keystroke enlarges it to read clearly, another restores it.

When sessions pile up, the list commands are your eyes. tmux list-sessions lists all sessions, tmux list-windows -t vibe253 lists one session's windows, tmux list-panes -t vibe253:editor lists one window's panes. Section 3 of the demo counts with exactly these three commands. There is also a shorter form: tmux ls is short for list-sessions. Build the habit of running ls before attaching, so you never attach to the wrong session.

send-keys is another command worth knowing. It "types" into a chosen pane: tmux send-keys -t vibe253:editor.0 'make test' Enter is the same as someone typing make test in that pane and pressing Enter. Combined with capture-pane, it enables scripted demos: send a command into a pane, wait a moment, then grab the pane's contents to see the result. The demo script follows this idea, except that for determinism it puts the commands directly in the new-session arguments.

The ~/.tmux.conf file is what makes tmux feel right. Without it, the defaults work, but almost everyone makes a few changes. First, enable mouse support with set -g mouse on, so panes can be clicked, borders dragged, and history scrolled with the wheel. Second, change the prefix key: some people switch Ctrl-b to Ctrl-a because it sits closer to the left hand — unbind C-b first, then set -g prefix C-a. Third, start window numbering at 1 with set -g base-index 1, so Ctrl-b 1 jumps to the first window, which feels more natural.

After editing the config, there are two ways to apply it: quit all sessions and re-enter, or run tmux source-file ~/.tmux.conf inside a live session. The second is the usual one: edit, source, and it takes effect immediately without restarting. The demo script writes no config file, because configuration is personal taste and everyone changes different things; the three changes above are the most commonly made, so copy them as a starting point.

History size is an easy thing to overlook. A pane keeps only the most recent output, 2000 lines by default. During a long task, the log grows and early output gets pushed out. Fix it in the config with set -g history-limit 50000, raising the cap to 50,000 lines. Then use Ctrl-b [ to enter copy mode, arrow keys or PageUp to scroll through history, and q to exit. In copy mode you can also select text with the mouse and copy it; paste with Ctrl-b ]. Copying and pasting inside a terminal has its own tmux way of doing things.

Naming habits for sessions are worth a word too. The demo uses vibe253, a "project plus number" name. In real use, a session is usually named after its project: one project, one session, with windows split by "edit, logs, test" and panes by "code, shell." Switch projects, switch sessions, and nothing interferes. A well-named session is findable at a glance in list-sessions.

A troubleshooting list will save time. First, attach says the session is not found: run tmux ls and check the spelling — session names are case-sensitive. Second, Ctrl-b does nothing: make sure you press Ctrl and b together as one chord, not two separate actions. Third, a pane disappears when its command exits: a pane's lifetime follows its command, so once the command ends the pane goes with it; to keep the pane, append sleep or run a long-lived process. Fourth, after SSH drops and reconnects, attach says the session is already attached: use tmux attach -d -t session-name, where -d kicks the old connection out.

Fifth, copying and pasting inside tmux differs from outside: selecting text in the terminal is the terminal's business, while text selected in tmux copy mode lives in tmux's own buffer, pasted with Ctrl-b ]. Sixth, the mouse wheel will not scroll: without mouse on, the wheel belongs to the terminal; with it on, it belongs to tmux — two different behaviors, do not mix them. Seventh, a config change has no effect: run source-file, or check that you edited ~/.tmux.conf and not some other path.

The session chooser is the smoothest entry point once sessions accumulate. Inside any session, Ctrl-b s opens a list of all sessions and their windows; pick one with the arrow keys and press Enter to attach. No need to detach first, no need to remember session names. Ctrl-b w does the same for windows. With many sessions and windows, these two choosers are far faster than typing commands.

Copy mode deserves a deeper look, because it is tmux's most underestimated feature. Press Ctrl-b [ to enter copy mode and the screen freezes: pane output stops scrolling, arrow keys move up and down, PageUp and PageDown flip pages, q exits. Inside copy mode, / searches — /error jumps to the next occurrence of error, n jumps to the next match. To copy, press Space to start selecting, Enter to confirm, then Ctrl-b ] to paste into the current pane. That flow — scroll old output, find an error, paste it elsewhere — is the standard way of working with a terminal.

Windows and panes can also be moved and swapped. tmux swap-window -s 0 -t 1 swaps windows 0 and 1, tmux move-window -s logs -t 2 moves the logs window to position 2. Panes have equivalents: tmux swap-pane -s 0 -t 1 swaps two panes, tmux join-pane moves a pane into another window, tmux break-pane splits a pane out into its own window. These commands are rarely used, but when a layout gets messy they are the only way to tidy it.

Session sharing is a rarely mentioned but useful feature. Two people attaching to the same session see the same screen, and what one person types the other sees live. It works for pair programming, helping a colleague debug remotely, or demonstrating a deployment. Both people simply run tmux attach -t with the same session name. One caveat: when the two terminals differ in size, tmux displays at the smaller size; to let each person use their own size, add set -g window-size largest or smallest to the config.

Remote development is tmux's classic use, and the flow is fixed: SSH into the server, tmux new -s project-name to create a session, set up edit, logs, and test windows, and start working; when you log off or the network drops, Ctrl-b d detaches and the session stays on the server; the next day, SSH in, tmux attach -t project-name, and everything is exactly as you left it. Builds, tests, and training runs keep going on the server the whole time, unaffected by the local machine powering off. Before tmux, remote work meant a pile of SSH connections, and one dropped connection killed everything; with tmux, the connection is just a window onto the session, and the session itself never stops.

Color and garbled-text problems will happen to everyone once. Inside tmux, wrong colors most often come from the TERM environment variable. Outside tmux, the terminal's TERM may be xterm-256color; inside, tmux changes TERM to screen or tmux-256color. Programs choose their colors from TERM, so a wrong TERM means wrong colors. Adding set -g default-terminal "tmux-256color" to the config fixes most cases. If colors are still wrong, check whether the terminal emulator supports 256 colors, and what TERM is outside tmux.

One more trap: the mouse wheel and copy-paste behave differently inside tmux than outside. Without mouse on, the wheel scrolls the terminal's own history and selection copies the terminal's way; with mouse on, the wheel scrolls tmux history and selection follows tmux copy mode. Each behavior makes sense on its own, but mixing them is confusing. The advice is to pick one: either keep mouse on and follow tmux's rules, or keep it off and follow the terminal's. Not half and half.

A typical layout for a vibe project is worth copying: the session is named after the project, window 0 is editor with two panes side by side — editor on the left, shell on the right; window 1 is logs running tail -f; window 2 is test running the test command. The three windows have fixed jobs, and over time they become muscle memory: Ctrl-b 1 for logs, Ctrl-b 2 for tests, Ctrl-b 0 back to the editor. Once the layout is fixed, the cost of switching contexts drops.

Finally, the name "terminal multiplexer" itself: tmux multiplexes not just the terminal but your attention — one project per session, one kind of task per window, one command per pane. Everything has its place, so finding things needs no history scrolling and no guessing which window runs what. The demo's 1 session, 2 windows, and 3 panes are the smallest example of this scheme. Scale it up a hundred times and the principle stays the same.

When you finish this guide, you should be able to do four things: create a named session, open windows and split panes, detach and reattach, and pull pane contents out with capture-pane. Three directions open up next: writing your own ~/.tmux.conf to tune the prefix key, mouse, and window numbering; running long tasks — training, builds, tests — on a server under tmux; and scripting with send-keys and capture-pane so tmux opens a ready-made workspace for you. None of them is urgent — run the demo script by hand first, and count 1 session, 2 windows, and 3 panes correctly before moving on.

Finally, a self-test list. First, create a session called test without looking at this article, then kill it. Second, recite the demo's numbers: 1 session, 2 windows, 3 panes, with the logs window printing 1 to 5. Third, build two windows in one session, split two panes in one of them, and count them with list-windows and list-panes. Fourth, detach once and reattach, confirming the screen is exactly as you left it. Fifth, write a three-line ~/.tmux.conf — mouse on, window numbering from 1, history limit 50000 — source it, and verify inside a session that clicking a pane with the mouse works.

tmux three layers: 1 session vibe253 with windows editor and logs; editor has 2 panes, logs has 1 pane, 3 panes in total
Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles