The Annoying Delete in Linux


When it comes to backspace and delete, linux SUCKS! BIG TIME!

I read a lot of howto online, but I don't think it is helping that 
much. Either they are awfully complicated or either it just works on 
specific system. Because of my bad long term memory, I want to write 
this down, so that I know what to do when I deal with this shit again.

I only care how to get bash, nano, emacs to work, so I guess you need 
to go somewhere else if you want to know how to configure other 
applications.

For bash, you need to know how bash interpreted your keys. To see what 
you typed, you do
Control V, then Key
Then you need to put the bindings in your .bashrc files
Nothing is more clear than an example
Say I press Control V, then backspace, it outputs ^H
and I press Control V, then del, it outputs ^?
do bind -p | grep delete may help too.
I put these lines in my .bashrc file

bind '"\C-h": backward-delete-char'
bind '"\C-?": delete-char'

For other weird symbol, such as control d and ^[[3~, you do 
bind '"\C-d": delete-char'
bind '"\M-[3~": delete-char'

It will be great if things are so easy, but usually they don't.
You may find your keys are interpreted differently in xterm.
You need to have specific settings for different environment.
My .bashrc looks like this

# remote login with putty or ssh secure shell
# ^H is a backward delete
# ^? is a forward delete
if [ "$TERM" == "vt100" ]; then
  bind '"\C-h": backward-delete-char'
  bind '"\C-?": delete-char'

# bind '"\C-d": delete-char'
# bind '"\M-[3~": delete-char'
fi

# for xterm
$ TERM may not be rxvt in xterm, so do echo $TERM to check
# ^? is a backward delete
# M-[3~ is a forward delete

if [ "$TERM" == "rxvt" ]; then
  bind '"\C-?": backward-delete-char'
  bind '"\M-[3~": delete-char'

#  bind '"\C-d": backward-delete-char'
#  bind '"\C-h": backward-delete-char'
fi

Similar deal with emacs. You need to find out what emacs interpreted 
what you pressed and you bind that with a function. Unfortunately, 
emacs and emacs -nw don't interprete keys the same way. So you need 
to have different settings for different environment.

To see what you type, you hit the key many many times, then do 
M x view-lossage. You should be able to see what emacs type, for 
both in xemacs mode and emacs -nw mode
Here is my .emacs file

;; use view lossage to see what keysym you are getting
(cond 
  ;; for window system
  (window-system
   (global-set-key (kbd "<backspace>") 'backward-delete-char)
   (define-key c-mode-map (kbd "<backspace>") 'backward-delete-char) 

   (global-set-key (kbd "<delete>") 'delete-char)
   (define-key c-mode-map (kbd "<delete>") 'delete-char))


  ;; for -nw / tty mode
  ('T
   (global-set-key "\C-d" 'backward-delete-char)
   (define-key c-mode-map "\C-d" 'backward-delete-char)

   (global-set-key "\C-h" 'backward-delete-char)     
   (define-key c-mode-map "\C-h" 'backward-delete-char)

   (global-set-key (kbd "DEL") 'delete-char)
   (define-key c-mode-map (kbd "DEL") 'delete-char)))




Written by Terence Tong
Statistics:
Go back to Home