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
  • 39,982
bpinaya
  • 405
  • I've just tried and replacing the snipped I used with just tmux works, but I get a: sessions should be nested with care, unset $TMUX to force. I've also tried leaving the snipped I used as it is and adding a tmux at the end of the ~/.bashrc but then detaching still exits the terminal. – bpinaya Feb 19 '19 at 10:37

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
  • 100,812