tmux `run-shell` eats your format strings — use `detach-on-destroy off` instead

A common pattern for "hop to another session before killing this one" looks like this:

bind Q run-shell 'next=$(tmux list-sessions -F "#{session_name}" 2>/dev/null \
  | grep -v "^#{session_name}$" | head -1); \
  if [ -n "$next" ]; then tmux switch-client -t "$next"; \
    tmux kill-session -t "#{session_name}"; \
  else tmux kill-session; fi'

It doesn't work. run-shell expands all #{...} format strings before handing the command to the shell. So list-sessions -F "#{session_name}" becomes list-sessions -F "mysession" — a literal string, not a format specifier. Every session prints "mysession", grep -v strips them all, $next is always empty, and you drop straight to bash.

The fix is one line:

set -g detach-on-destroy off
bind Q kill-session

detach-on-destroy off is a native tmux option: when a session is destroyed, the client automatically switches to another surviving session. It only falls back to exit when there's nothing left. No shell escaping, no format-string footguns, and it covers every exit path — not just prefix+Q.

Comments

  1. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.