How to close tmux session without exiting tmux?
I have several sessions in tmux
How to close/kill some of them, without exiting tmux?
If I type exit I am not only closing session, but also exiting tmux, which is not applicable.
2 Answers
I needed exactly this, as my workflow includes short-lived tmux sessions to jump between code projects that are killed at the end.
Since I couldn't find anything I wrote a script that switches to the first other active session before killing the current one and I bound it to Leader X for quick access.
~/.scripts/tmux-kill-session.sh:
#!/usr/bin/env bash
tmux_running=$(pgrep tmux)
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then exit 0
fi
session_to_kill="$(tmux list-sessions | sed -n '/(attached)/s/:.*//p')"
session_to_switch="$(tmux list-sessions | sed -n '/(attached)/!s/:.*//p' | head -n 1)"
if [[ ! -z $session_to_switch ]]; then tmux switch-client -t $session_to_switch
fi
tmux kill-session -t $session_to_kill~/.tmux.conf:
bind X run-shell "~/.scripts/tmux-kill-session.sh" You can kill sessions directly within tmux. Let's say you have two sessions: session1 and session2. And you're currently in session1.
Use this, while still in session1, to kill session2.
tmux kill-session -t session2You should remain within session1. And if you should no longer see session2 in your session list.
2