Knowing how to close tmux correctly depends on what you want to close: a pane, a window, a tmux session, or the entire tmux server. You may want to leave a session running in the background, or you may want to stop every process managed by tmux. This guide explains the safest keyboard shortcuts and commands for each situation, including how to detach from tmux without ending your work.
The most important distinction is that detaching is not the same as closing. Detach when you want to leave a session available for later; kill a pane, window, or session when you intend to end it. With the right command, you can close exactly the part of tmux you mean to close.
How to Close a Tmux Pane
A pane is one section of a tmux window. To close the pane you are currently using, type exit in its shell and press Enter. You can also press Ctrl-D, which sends an end-of-file signal to the shell. If a program is still running in the pane, stop or exit that program first; otherwise, the shell may not be ready to close.
You can also use tmux’s built-in kill-pane command. Press the tmux prefix key, release it, and then press x. The default prefix is Ctrl-B, so the full shortcut is Ctrl-B, followed by x. Tmux asks you to confirm before it closes the active pane. Confirm only if you are ready to end any foreground work there.
For a command-line alternative, open the tmux command prompt with Ctrl-B, then :, and enter:
kill-pane
This closes the active pane. To close a specific pane instead, target it by its pane ID or use a target such as session:window.pane. List panes and their IDs with tmux list-panes. Closing a pane ends the processes running inside it, so save files and check for important jobs before using a kill command.
How to Close a Tmux Window
A tmux window can contain one or several panes. To close the current window, first make sure it is the window you intend to remove. One straightforward method is to exit each shell in its panes. When the final pane in a window closes, the window closes too.
You can also kill the current window from the tmux command prompt. Press Ctrl-B, then :, type the command below, and press Enter:
kill-window
To close a different window, add a target. For example, kill-window -t work:2 closes window 2 in the session named work. Use tmux list-windows to check window names and numbers before running a targeted command. A window kill affects every pane in that window, so it can stop several processes at once.
How to Close a Tmux Session
A session groups tmux windows together and is often the main thing people mean when they say they want to close tmux. To end the current session, run exit in its remaining shell, or use the tmux command prompt and enter:
kill-session
This terminates the current session and its windows and panes. Any programs running in those panes may also be stopped. If you have more than one session, closing one does not normally close the others.
To close a particular session by name, use:
tmux kill-session -t work
Replace work with the session name you want to end. To see available sessions first, run tmux list-sessions, or use the shorter command tmux ls. Checking the list is a good precaution, especially if your sessions contain long-running jobs or unsaved work.
You can also close a session from within tmux by opening the command prompt with Ctrl-B, then :, and entering kill-session. To target a named session from that prompt, enter kill-session -t work.
How to Close the Entire Tmux Server
If you want to end every tmux session managed by the current server, use:
tmux kill-server
This is the most comprehensive way to close tmux. It stops all sessions on that server, including their windows and panes, and disconnects attached clients. Use it only when you are certain you do not need any of the server’s sessions. It is not the right choice if you only want to leave your current session or close one window.
To check what would be affected, run tmux list-sessions before killing the server. If you use multiple tmux servers with different sockets, make sure your command points to the server you intend to stop. A server-level command can affect more work than expected, particularly on shared or remote systems.
How to Detach From Tmux Without Closing It
If your goal is to leave tmux but keep your work running, detach instead of closing the session. The default shortcut is Ctrl-B, followed by d. Tmux returns you to your normal terminal while leaving the session running in the background. Processes in the session can continue, and you can attach again later.
To reconnect to a session, use tmux attach if there is only one suitable session. If you have several, specify the session name, for example:
tmux attach-session -t work
Think of detaching as putting a session aside, not deleting it. This is useful when you are closing a terminal window, disconnecting from a remote machine, or temporarily switching tasks. If you choose exit or kill-session instead, the session will be ended rather than preserved for reattachment.
You can also detach from the tmux command prompt with detach-client. This disconnects the current client without killing the session. For a specific client or session, consult tmux list-clients and the tmux manual for the appropriate target options.
Quick Tmux Close Commands
Use this quick reference to choose the action that matches what you want to close:
-
exitorCtrl-D: close the shell in the active pane. -
Ctrl-B, thenx: prompt to close the active pane. -
kill-pane: close a pane from the tmux command prompt. -
kill-window: close the current window and its panes. -
kill-session: end the current session. -
tmux kill-session -t name: end a named session. -
tmux kill-server: end all sessions on the selected server. -
Ctrl-B, thend: detach while keeping the session running.
What Happens When You Type Exit in Tmux?
Typing exit closes the shell in the pane where you type it. What happens next depends on how many panes and windows remain. If the pane is one of several in a window, that pane closes while the other panes stay open. If it is the last pane in its window, the window closes. If that was the session’s last window, the session ends.
This behavior makes exit convenient when you want to close a shell naturally, but it may not be the best option for closing a particular session in a busy tmux setup. When you need precision, use kill-session -t name or another targeted command after checking the session list.
Closing Tmux When You Are Connected Remotely
Tmux is commonly used over SSH because it can keep work available after a network connection drops. If you simply close your SSH client, tmux sessions may remain on the remote server. Reconnect to the same machine and run tmux ls to check whether the session is still there, then attach with tmux attach -t name.
If you want the remote work to continue, detach with Ctrl-B, then d, before disconnecting. If you want to stop the remote session, use tmux kill-session -t name while connected. Be careful not to confuse closing your local terminal with terminating a remote tmux session; those actions can have different results.
Closing Tmux Inside Another Tmux Session
You can run tmux inside a tmux session, sometimes called nested tmux. In that situation, your prefix key may be handled by the outer tmux instead of the inner one. If both use the default prefix, press Ctrl-B twice to send the prefix through to the inner session, then press the desired key. For example, use Ctrl-B, Ctrl-B, then d to detach the inner client in a typical default configuration.
Since nested setups can use custom prefixes, the exact key sequence may differ. If the shortcut affects the wrong session, use the tmux command prompt or run a targeted command in the relevant shell. Check which tmux client or server you are interacting with before issuing a command that kills a session or server.
Troubleshooting Common Tmux Closing Problems
If a pane does not close after you type exit, a foreground program may still be running, or the shell may not have received the command. Return to the shell prompt, stop the program gracefully, and then try again. If the program is unresponsive, use Ctrl-C as appropriate, or close the pane with Ctrl-B, then x, understanding that this may terminate its processes.
If tmux kill-session -t name reports that no session matches, check the exact spelling with tmux ls. Session names are not always the same as the directory or project name you expect. You can create named sessions with tmux new -s name, which makes them easier to identify and target later.
If tmux says there is no server running, there may be no active sessions for that socket, or you may be using a different tmux socket than before. For ordinary use, start or attach to the appropriate tmux server. Avoid repeatedly running kill commands without checking the server and session list, as that can make it harder to determine which environment you are affecting.
How to Choose the Right Way to Close Tmux
Use the smallest action that accomplishes your goal. Close a pane if you only want to remove one shell, close a window if you want to remove all of its panes, and kill a session if you are finished with that group of windows. Use tmux kill-server only when you intend to stop every session on the server.
When you are unsure, detach first. Detaching with Ctrl-B, then d preserves your session and gives you time to confirm that nothing important is still running. You can list sessions later and remove the ones you no longer need.
Conclusion
To close tmux, first decide whether you want to end a pane, window, session, or the entire server. Use exit or Ctrl-D for a shell, kill-window for a window, kill-session for a session, and tmux kill-server to stop all sessions. If you only want to leave while preserving your work, press Ctrl-B, then d to detach. These distinctions help you close tmux safely without accidentally stopping processes you meant to keep running.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.