How to prevent iterm2 from closing when typing Ctrl-D (EOF)

Shell approach

After reading this question and answer I updated my delete-or-exit function to ask for confirmation rather than completely deactivate it:

cd ~/.config/fish/functions/
cp /usr/share/fish/functions/delete-or-exit.fish .

Then edit/replace:

function delete-or-exit

    set -l cmd (commandline)

    switch "$cmd"
        case ''
            read --nchars 1 --local -P 'Do you want to exit? [y/N] ' confirm
            switch $confirm
                case Y y
                    exit 0
                case '' N n
                    echo -n (fish_prompt)
            end

        case '*'
            commandline -f delete-char
    end
end

It has a minor issue in that it displays the prompt twice when you finish, but it seems better than no times if you don't print it (see N case above). Perhaps someone has a solution to that.

Terminal approach

iTerm and many other terminals have a setting says something like, "keep terminal open when program exits."


This is the default key binding for control-D:

bind \cd delete-or-exit

you can find this by just running bind.

(delete-or-exit is just a function, which you can read with functions delete-or-exit.)

So it's exiting because that's what the default behavior is. You can make control-D do something else. For example, maybe it should delete the character under the cursor:

bind \cd delete-char

If you want to make this permanent, add it to your fish_user_key_bindings function:

  1. Run funced fish_user_key_bindings which starts editing
  2. Put bind \cd delete-char within the function
  3. Hit return to create the function
  4. Run funcsave fish_user_key_bindings to save it