How can I prevent tmux exiting with Ctrl-d?

32

8

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.

Cas

Posted 2012-09-26T01:41:29.463

Reputation: 1 600

Answers

40

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

chepner

Posted 2012-09-26T01:41:29.463

Reputation: 5 645

7That 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 – 2012-09-26T10:43:21.737

7Setting IGNOREEOF environment variable have no effect in zsh, but "set -o ignoreeof" or "setopt ignoreeof" does. – sgtpep – 2013-04-24T06:19:49.213

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.

Rohmer

Posted 2012-09-26T01:41:29.463

Reputation: 281

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 – 2017-08-21T22:51:06.540

1I 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 – 2018-03-25T02:42:33.870

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 – 2018-04-02T10:01:39.793

*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 – 2019-01-20T08:31:41.203

2@Rohmer, prezto taught me to use exec zsh to restart zsh within tmux when I'm playing with the .zshrc – krry – 2019-02-19T20:58:11.633

Your solution is very smooth, just replaced the exit to complete session detach, love it! Thanks! – Soullivaneuh – 2019-02-27T23:44:23.573

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'

Thor

Posted 2012-09-26T01:41:29.463

Reputation: 5 178