8

I've used this snippet to launch tmux when the terminal is launched:

# TMUX startup
if command -v tmux>/dev/null; then
 [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && exec tmux
fi

But with this I can't exit tmux without the terminal screen being closed too.

I've tried:

Ctrl + b :detach

exit

And looking for the PID and killing it. All those methods close the terminal too.

How should I configure tmux to start when launching the terminal but still being able to close it without the terminal closing? Any tips are appreciated!

dessert
  • 40,956
bpinaya
  • 395
  • 1
  • 5
  • 13

1 Answers1

11

The problem is the exec command. As explained here, exec will replace the current shell with whatever you tell it execute. So you don't have a shell that is running tmux, you just have tmux and therefore exiting it will also exit the terminal.

Just remove the exec and it should work as expected:

if command -v tmux>/dev/null; then
 [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && tmux
fi
terdon
  • 104,119