Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing lisp emacs key binding and cannot specify the <delete> character

For some reason I got the default M-del key binding for backward-kill-word mapped to a scan for matching brackets and resetting is not working, so I am trying to set the global key binding in lisp. So I wrote in ~/.emacs.d/init.el the lisp commands:

(global-set-key (kbd "M-h") 'backward-kill-word)

(global-set-key (kbd "M-<\delete>") ‘backward-kill-word)

I tried them with C-x C-e and they both give the 'backward-kill-word output but only the first key-binding works "M-h", the other is ignored and M-del still trying the strange scanning action. The delete key works in emacs elsewhere, so it seems like "delete" is not being mapped to the physical key in lisp (and the backslash is there to show in this text only as the word was being commented out). Any idea what keyword to use or special character? Best.

(I looked for libraries that may have overrided this command but I cannot find them)

like image 344
Vass Avatar asked Jan 22 '23 13:01

Vass


1 Answers

On some systems, the delete key is defined as an alias to C-d. This is done through function-key-map on GNU Emacs <23 and local-function-key-map on GNU Emacs 23. (I've observed this behavior on Debian and Ubuntu 10.04 under X.) The purpose of such translations is to isolate people who code modes from the terminal intricacies: a mode that wants to shadow the delete command only needs to rebind C-d and not wonder if it should rebind delete (is that a delete left or delete right?) or deletechar or something else.

If there is a global or local binding for delete, it shadows this translation to C-d. However, if you press ESC delete, if there is no global or local binding for ESC delete, the second key is translated to C-d. This translation has precedence over the interpretation of ESC delete as M-delete. So ESC delete becomes equivalent to C-M-d.

This is arguably a bug in Emacs: the effect of ESC delete should be the same as M-delete, and there is no reason why ESC delete would run down-list which has nothing to do with deletion.

There are several possible fixes; I don't know which is best. One that should work with any version of Emacs is

(global-set-key [?\e delete] 'backward-kill-word)
like image 63
Gilles 'SO- stop being evil' Avatar answered Jan 29 '23 08:01

Gilles 'SO- stop being evil'