How to prevent iterm2 from closing when typing Ctr

2019-06-25 10:27发布

问题:

I am using fish shell. When I type Ctrl-D, it sends a EOF to my terminal and then terminal closes.

I want to make it such that ctrl-D does not close my iterm2.

I saw that people have set up IGNOREEOF in bash shell like this: https://unix.stackexchange.com/questions/27588/how-can-i-keep-controld-from-disconnecting-my-session

However, I don't think this variable exists in fish. Does anybody know how I can force iterm2(with default fish shell) to not close on ctrl-D?

回答1:

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


回答2:

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.