Delete key doesn't work on command line
For Googlers:
Whew. For something that should be so simple, this was hard.
The short solution is use the following to set the delete key (in kshrc
or wherever),
bind '^[[3'=prefix-2
bind '^[[3~'=delete-char-forward
And set your PuTTy terminal settings to rxvt
instead of Standard
.
This thing that really helped me get this working was: http://www.mail-archive.com/[email protected]/msg81796.html
ksh does silly things with the home and end keys. Basically, I couldn't make it tell the difference between Home, End, and Delete at the same time. Whatever was last bound, all three keys would do. Changing what PuTTy sent for these keys helped immensely.
Note: Some folks suggest if you want to see what code the shell is getting when you press a key, type cat
, press enter, then push the key. For my shell, this didn't work. I got ~
for all the control keys. What I did instead was press Esc
once, then press the key. The control code would show then show up. Use that control code in bind
and you're all set.
If shell is running inside a gnome terminal window, then menu into: Edit
| Preferences
| Profiles
and select a profile, then select the Compatibility
tab, and change the Delete key generates
to Automatic
. (Or if this fails try the other choices there.)
Also, you can enter ^v Del
and ^v Backspace
, (i.e. Control-v followed by the delete key, and Control-v followed by the backspace key), to find the returned 'terminal' sequence codes.
And for inspection use the showkey
command as showkey -s
, showkey -k
, and/or showkey -a
(i.e. the three layers) and then press the Del
and Backspace
keys for:
I) raw output from a keyboard
II) output from tty driver
III) character strings given to a (ansi) terminal
From this I noticed that when using stty (e.g. stty1) that my shell behavior was different from when using xterm (from within a Graphical X terminal). Del deleted correctly forwards (right) in stty1, but backwards (left) in my xterm.
The existing two answers did not work for me going from Linux (Ubuntu 18.10) bash, via SSH to Solaris 11.3 bash, using gnome terminal.
I found I needed to use the bind
command, but with a work-around, as I could not get a native Delete to function.
So the work-around is that when Delete is pressed, to simulate a delete by mapping the delete keypress to → and Backspace.
bind '"^[[3~":"^[[C^?"'
To type that, use the key-presses:
CTRL-vDelete for the first part
and CTRL-v→CTRL-vBackspace for the second.
(or use \e
for the escape instead, e.g.: "\e[3~"
)
It's not perfect, if you Delete at end-of-line, it still backspaces. But it saves me having to backspace-out the ~
character umpteen times a day.