Mac – Terminal on Mac – Delete key behavior

backspacefn-keymacterminal

I'd like the delete key (well, the Fn+Backspace combination) on my Mac to behave the same way in the Terminal as it does normally. That is, to do forward-delete. Right now it outputs tilde – at least that's what I can see.

In Binding Fn-Delete in zsh on Mac OS X I saw some hackery and tried it too:

(pressed [Fn]+[<—] inside the quotes below)

$ echo "~" | od -c
0000000   ~  \n
0000002

How can I make it behave?

Best Answer

The answer to the question to which you linked shows how to find the control sequence Terminal sends when you press FnBackspace: echo 'ControlV FnBackspace' | od -c.
The ControlV is critical to prevent special interpretation of the (likely) initial ESC character.

Terminal probably sends the four byte sequence ESC [ 3 ~.

The question to which you linked was asking about zsh. The comment on the answer gives the command to bind the sequence in zsh, i.e:

bindkey "^[[3~" delete-char

(usually in ~/.zhsrc).

However, bash is the default shell on Mac OS X, so the command to bind a key (and the functions available for binding) will be different if you are using bash: bind '"\e[3~": delete-char'. You will probably want to put this in a bash startup file1.

If you find that you are using bash, but you want to use zsh instead, then there are two ways to change your effective shell:

  • Use chsh -s /bin/zsh to change your default shell.
    This will change the shell that Terminal starts as well as the shell started for other login sessions (e.g. logins through SSH).
  • Configure just Terminal to use a different shell in Terminal’s preferences.
    Terminal > Preferences…, Settings tool bar button, then the Shell tab,
    change Run Command to (e.g.) /bin/zsh -l.

1 Usually ~/.bashrc, but you can also put a related line ("\e[3~": delete-char) in ~/.inputrc instead. If you put it your .bashrc, you will want to make sure that you also have a line like source ~/.bashrc in ~/.bash_profile, or ~/.bash_login (if you have neither, then create the former; if you already have exactly one of them, then use the one you have; if you have both, then you should fix that since probably only the former is being used).

Related Question