Disable CTRL-D from closing my window with the terminator terminal emulator)

24,854

Solution 1

You can also disable eof generally in bash:

set -o ignoreeof

Solution 2

You can use the IGNOREEOF environment variable in bash. So export IGNOREEOF=42 and you'll have to press Ctrl+D forty-two times before it actually quits your shell.

POSIX set has an -o ignoreeof setting too.

Solution 3

The Ctrl-D EOF character is being interpreted by the shell, rather than the terminal emulator specifically. Other answers have covered bash settings, although other shells are different.

For C-shells (e.g. tcsh) you can add it to your tcshrc file:

# Prevent accidental logouts completely
set ignoreeof
# Just prevent the first two, and allow the third
set ignoreeof=3

For fish shell, the Ctrl-D behaviour is controlled by the keybinding. The default setting is delete-or-exit, so you can set the keybinding for \cd to delete-char to only support delete.

More details are in the Fish Github issue (e.g. in versions before 3.0 you need to add the bind to a function called fish_user_key_bindings, after 3.0 you can put it into ~/.config/fish/config.fish) but in summary:

bind \cd delete-char  # Don't exit on accidental Ctrl-D
bind \cd\cd\cd delete-or-exit  # Exit on the third one
Share:
24,854

Related videos on Youtube

Tam Borine
Author by

Tam Borine

Updated on September 18, 2022

Comments

  • Tam Borine
    Tam Borine over 1 year

    I am often logged in several SSH sessions at once. To logout from several sessions, I press CTRL+d, until I am back on my local machine.

    However, I occasionally press it once too many, and my terminal exits.

    Is there a way to make CTRL+d unable to close my terminal ?

    I am using terminator as my terminal emulator.

  • deltab
    deltab almost 10 years
    Forty-three times: the first forty-two are ignored, and the last one actually quits. :-)
  • Matej Vrzala M4
    Matej Vrzala M4 almost 10 years
    I figured Forty-three times would be too redundant. Definately the set -o ignoreeof would be more practical
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 10 years
    But if you do that indiscriminately, it'll apply when logged in over ssh as well.
  • Jordan Singer
    Jordan Singer over 9 years
    But you have to write the command every time you open a terminal.How do you disable this permanently?
  • AAAfarmclub
    AAAfarmclub over 7 years
    I'm using terminator 0.97. When I do set -o ignoreeof, it blocks ctrl-d (doesn't exit) 10 times with a message: Use "exit" to leave the shell. On the 11th ctrl-d, it exits.
  • Victoria Stuart
    Victoria Stuart about 6 years
    OMG, thank you!! That (accidental terminal cloures) was a huge annoyance for me. Added to ~/.bashrc; works in konsole | xfce4-terminal | ... :-D
  • Gauthier
    Gauthier over 5 years
    @Gilles Good input, do you have a solution for that?