172

I am using tmux to manage multiple terminal windows. Currently I have 2 windows open with multiple panes. I know that I have to use the prefix (for me it is ctrl+a) to enter commands.

How can I close the session from inside of tmux? What command would close the session (all windows and panes) and tmux at the same time?

fixer1234
  • 27,064
  • 61
  • 75
  • 116
ph3nx
  • 1,823
  • 2
  • 12
  • 6
  • 3
    To detach from tmux (leaving process running): https://unix.stackexchange.com/a/174443/178412 – Lee Jul 17 '19 at 11:39
  • 3
    One very handy way to kill multiple sessions at once is first go to the overview window (with `Ctrl`+`a`->`s`) and then press `x` with the target section selected, followed by `y` to confirm. No typing `:kill-session`, no rebinding – rtviii Jul 08 '21 at 07:03

6 Answers6

200

Press your prefix (e.g. Ctrl+A or B, which is the default) and then : and type kill-session, then hit Enter. This will, as the name of the command suggests, kill the session.

0xC0000022L
  • 6,819
  • 10
  • 50
  • 82
  • 9
    `exit` can be used instead of `kill-session` too – alizeyn Mar 05 '20 at 08:53
  • 8
    `exit` will only close, e.g., the bash that tmux has opened. When you only have one panel open this may also close the window - not so for two panels open within one window. – dgruending Oct 08 '20 at 08:43
  • 3
    Here are useful bindings just in case someone stumbles on this issue. Add the following to your `.tmux.conf` `bind C-x confirm-before -p "kill other windows? (y/n)" "kill-window -a" ` `bind Q confirm-before -p "kill-session #S? (y/n)" kill-session` – user79262 Mar 31 '21 at 23:15
  • 1
    A better way (AFAIK the correct answer to the OP): `Ctrl`+`B`, then `&` and confirm with `y` – Antonio Jul 02 '22 at 16:57
  • 2
    @Antonio which kills the window and not the session. – Martin Braun Jan 26 '23 at 19:21
  • @MartinBraun, you're right, but the OP talked about only one session. In this case, one session and multiple windows with multiple panes, closing the window also close the session. – Antonio Jan 28 '23 at 16:05
  • 1
    @Antonio He has more windows in one session. `&` will only kill one of his windows and not his session. He has to repeat the steps you provided for each window to kill the session eventually, which is not really efficient on many windows. `:kill-session` is the right answer, or my personal favorite: `prefix`, `s` then `x`, `y` (see my answer for details). – Martin Braun Jan 28 '23 at 17:37
  • I love Unix things because there's always more way to obtain the same result. :-) – Antonio Jan 29 '23 at 11:41
  • @Antonio in this case don't forget to vote on Martin's answer, too – 0xC0000022L Jan 29 '23 at 20:55
  • 1
    I did it! Also to te comment. ;-) – Antonio Jan 30 '23 at 10:12
80

If you want to close session other than the current session (and hence keep tmux running; closing the current session as in @0xC0000022L's answer, also closes tmux even if other sessions are available), do the following in one of the tmux windows:

# tmux ls
keepMe: 1 windows (created Wed Jun 24 14:20:15 2015) [171x41]
otherSession: 1 windows (created Wed Jun 24 14:22:01 2015) [171x41]
3: 1 windows (created Wed Jun 24 14:23:28 2015) [171x41]

(assuming here that you're on keepMe session)
# tmux kill-session -t otherSession
-or-
# tmux kill-session -t 3

This deletes the "otherSession" or session number 3, and leaves tmux running, assuming you didn't have "otherSession" or number 3 session selected when you deleted them.

Ville
  • 2,892
  • 2
  • 22
  • 20
  • 4
    Or if you prefer using your prefix, `ctrl + prefix` followed by `:` and then type `kill-session -t name` or `kill-session -t number`. If you do this from within the session you want to kill reattach using `tmux a`. – Matijs Apr 14 '16 at 10:03
  • Related: https://tmuxguide.readthedocs.io/en/latest/tmux/tmux.html – Artfaith Dec 04 '21 at 18:13
  • Is it possible to use __entire__ session name to kill the target session? I remember that `-t` will match on prefix of any length. – Niing Dec 16 '21 at 15:39
44

TMUX Prefix (e.g. ctrl+b) + :kill-session

or

tmux kill-session (could run either from the inside of a session or the outside)

Both styles of invocation can use the flags:

-t target-session destroys the given session
-a destroys all sessions but the given one or the one you are attached to

Running kill-session from the outside of TMUX kills the last session you were attached to. -a inverts that.

In the event, that you have more than one session running you could kill all sessions at once with: kill-server.

FSchndr
  • 541
  • 4
  • 5
  • This is similar to the current accepted answer, can you go into a little more detail on why your answer is different? – Burgi Dec 29 '16 at 12:37
  • 2
    No one pointed out, that you could close all sessions with the `kill-server` command. I also liked to provide a little more depth on the flags for `kill-session`. – FSchndr Dec 29 '16 at 14:34
  • 6
    +1: This answer is also the only one with the helpful `-a` to destroy all other sessions. – AaronDanielson May 24 '17 at 13:53
  • This is the better answer. I wish this were on the top of the answers... – Blaszard Jul 04 '18 at 11:31
9

One of the best way to do this interactively is by pressing ctrl+b,w to go to window mode then add tag by pressing t. then press capital X to kill all tagged window...

enter image description here

this is apparent when you have mouse enabled. enter image description here

Vaisakh K M
  • 146
  • 1
  • 3
3

If you want to kill one session and all its windows and panes conveniently, just press PREFIXs. Then navigate to the session and hit x and confirm with y.

Killing a session using prefix s and then x y

(^A is my prefix)

You can also tag more than one session using t and then killing the tagged ones with X.

If you kill the current session, tmux will be exited as well. If you don't wish to exit, simply switch a different session before killing the session in question.

Martin Braun
  • 681
  • 2
  • 10
  • 22
-1

Unfortunately there does not appear to be a simple default keybind to do this, possibly because it's an action you might want to be careful about though this could be addressed with a confirm dialogue.

One way is C-x : to get the tmux prompt and kill-session

As @Vaisakh-k-m pointed out C-x w gives you an interface to select windows across multiple sessions which you can select with the arrow keys and t to toggle the tagged status then X to kill the tagged windows and y to confirm this action.

This way there is no need to type a command at the prompt (accessed with C-x :) kill-server kills all sessions not merely the current one

If you'd like a custom kill session keybind @user79262 suggests these additions to your .tmux.conf:

bind C-x confirm-before -p "kill other windows? (y/n)" "kill-window -a"
bind Q confirm-before -p "kill-session #S? (y/n)" kill-session

You can close the current window with: C-x (Your prefix Ctrl+a in the case of the OP, Ctrl+b by default) then & and confirm with y This shortcut kills the window but not the current session unless your are in the last open window.

If you run tmux ls you'll see that with C-x d the session is still running in the background. You can re-attach to these background sessions with tmux a -t <session> where session is a name number.