Terminal on Mac - Delete key behavior

7,899

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).

Share:
7,899

Related videos on Youtube

Martin Janiczek
Author by

Martin Janiczek

Updated on September 18, 2022

Comments

  • Martin Janiczek
    Martin Janiczek about 1 year

    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?

    • Chris Johnsen
      Chris Johnsen over 12 years
      The comments on SU 169930 seem fairly clear: You should be using Control-v before pressing Fn-BackSpace, which will probably show up as 033 [ 3 ~ from od. Then use bindkey "^[[3~" delete-char to bind what Terminal sends to a function in zsh. Does that not work for you?
    • Martin Janiczek
      Martin Janiczek over 12 years
      Tried to add bindkey "^[[3~" delete-char into my ~/.profile, restarted Terminal, and tells me it doesn't know bindkey. Added it into .zshrc, restarted Terminal, and nothing happens. I don't really think I'm using zsh. Or is it Mac's default shell? I'm an amateur in these things... :)
    • Chris Johnsen
      Chris Johnsen over 12 years
      You can check your shell with dscl . read /Users/$USER UserShell. The default is bash. You can change your default shell with chsh -s /bin/bash. You can change just the shell used in Terminal in its preferences (Settings tool bar button, then Shell tab, change Run Command to (e.g.) /bin/zsh).
    • Martin Janiczek
      Martin Janiczek over 12 years
      Yes, my default is bash. I tried zsh and it seems to work the way I want it to. Does the bash have a way to achieve the same result?
  • Martin Janiczek
    Martin Janiczek over 12 years
    Excellent explanation. Thank you! (oh, and it worked. :) )
  • itsthejb
    itsthejb almost 7 years
    Nice simple answer for me. Just put this in ~/.zshrc on the server