12

I am using iTerm2 on OSX, and I am using an external keyboard with a Del (not to be confused with Backspace). The Del key works fine in OSX but in Terminal and iTerm, it inserts "~" instead of deleting forwards one character. This is easily fixable in Terminal, but in iTerm, there is no key binding or escape sequence that allows me to delete a character to the right of the cursor (the equivalent Unix command is Ctrl D). Is there any way I can simulate deleting forwards one character in iTerm?

luxon
  • 417
  • 5
  • 14

3 Answers3

23

Like Gajus said, using Ctrl-D to delete one backspace forward also closes the terminal tab if there's no input which can be very frustrating.

I am using zsh, but adding the lines ricpacca suggested to .zshrc didn't work for me.

What I did instead is to map Ctrl-Delete to send the hex codes "0x06 0x08".
0x06 translates to "move one character forward", and 0x08 is just a normal backspace.

Thought I'd share my solution for any other frustrated iTerm2 users that scoured the internet to no avail.

stav meir
  • 393
  • 2
  • 8
  • 4
    Brilliant! You're a hero. Thanks. This should be the accepted answer since it's so much less intrusive than using `0x04`. – philraj Sep 20 '18 at 18:57
  • 1
    Just a heads up for others, two drawbacks of this method are: 1. if you reach the end of the line while deleting, this will work like a plain backspace, i.e. deleting characters before the cursor and not after. 2. It breaks the del-key functionality in vi/vim and most probably in every other application that has a special use for del. Especially the latter is a dealbreaker for me. Unfortunately I have no solution other than switching to Hyper, where the key simply works out of the box. – Dantel35 Apr 07 '21 at 11:56
22

If you are using zsh, try adding these lines to your ~/.zshrc file, then close and reopen the terminal:

bindkey    "^[[3~"          delete-char
bindkey    "^[3;5~"         delete-char

These should enable your delete key on any keyboard, without manual keyboard mappings to the hex command 0x04, which has the problem that it closes the session if the input is empty.


Why this? Because it seems that zsh doesn't read /etc/inputrc with the key bindings by default.

ricpacca
  • 800
  • 6
  • 9
15

You can add a iTerm Keyboard Mapping in Global Preferences or a Profile-based one for the key to send a Ctrl-D (0x04)

i.e: Here I am mapping Ctrl-Delete to Erase (Ctrl-D/0x04) like an Extended Keyboard's Del key:

enter image description here

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • 11
    The problem with `0x04` is that it closes the tab if the input is empty. When this happens by an accident, it is incredibly frustrating. – Gajus Jan 05 '17 at 14:29
  • It's only a problem if you're not used to it. Coming from linux, it's the exact behavior I was looking for. – PierreE Apr 02 '22 at 02:30