30

I use tmux on my server and recently I found to my cost that ctrl-d will exit tmux and lose all the session information, my intention was to simply end the ssh session but failed to notice I was still in tmux until too late.

I am aware that I should be careful in future when using ctrl-d but I wondered if there a way to prevent tmux for exiting when hitting ctrl-d by accident? A solution such as a prompt, confirmation or detaching would be fine.

36

To be precise, Ctrld does not exit tmux, but rather a shell. If that shell is running in the only pane of the last window in the tmux session, the session ends and the tmux client exits.

To prevent Ctrld from exiting the shell, you can set the IGNOREEOF shell variable, or set the ignoreeof shell option. Put one of the following in your .bashrc file:

IGNOREEOF=10   # Shell only exists after the 10th consecutive Ctrl-d

set -o ignoreeof  # Same as setting IGNOREEOF=10
  • 6
    That is excellent, thanks. I only needed it for tmux so I used set-environment -g 'IGNOREEOF' 2 in .tmux.conf and now I need to hit Ctrl-d 3 times to exit and I am also warned each time with the message: Use "logout" to leave the shell. – Cas Sep 26 '12 at 10:43
  • 7
    Setting IGNOREEOF environment variable have no effect in zsh, but "set -o ignoreeof" or "setopt ignoreeof" does. – sgtpep Apr 24 '13 at 6:19
12

IGNOREEOF didn't work for me so I just bound Ctrl+D to detach in .tmux.conf:

bind-key -n C-d detach

The -n means no prior escape sequence needed, like the tmux prefix.

  • A year and a half later, IGNOREOF didn't work for me before since I'm using zsh. setopt ignoreeof in .zshrc works. But I had to kill all tmux sessions for tmux to source .zshrc. Maybe I could have sourced .zshrc from within tmux... – Rohmer Aug 21 '17 at 22:51
  • 1
    I have been using this, but it has an annoying limitation: it detaches on any Ctrl+D, whether it's being sent to the shell or to something like cat > some_new_file.txt. – Mihai Danila Mar 25 '18 at 2:42
  • This, however, has a side effect in tmux with more panes open as it doesn’t let you close a pane with ^D. – Mr. Tao Apr 2 '18 at 10:01
  • UPDATE For those who want only to keep the last window/pane: bind -n C-d if-shell -b 'USHELL="$(basename "$(getent passwd $USER | cut -d: -f7)")"; [ $(tmux list-windows | wc -l) -eq 1 -a $(tmux list-panes | wc -l) -eq 1 -a $(pstree $PPID | egrep "\\b$USHELL\$" | grep -o $USHELL | wc -l) -eq 1 ]' detach 'send C-d' – Frederick Zhang Jan 20 at 8:31
  • 1
    @Rohmer, prezto taught me to use exec zsh to restart zsh within tmux when I'm playing with the .zshrc – krry Feb 19 at 20:58
7

Besides chepner's answer you can stop the terminal from sending EOF entirely by setting eof to undef with stty:

stty eof undef

Reset with:

stty eof '^d'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.