views:

68

answers:

4

Hello, 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)

A: 

You might want to call global-set-key interactively to see how it interprets meta-delete. Also try local-set-key to ensure the strange binding is not mode-specific.

Hemal Pandya
trying local-set-key as well does not work
Vass
Is this happening only for meta-delete or other key sequences as well? Try starting emacs as `<emacs> --no-site-file --no-init-file`. First check if meta-delete works as expected. If it does, drop each option in turn to identify the init file that causes this. I wonder if that will help, since you are even able to set key locally, but its worth a try.
Hemal Pandya
thanks for the reply comment, above Gilles has the answer which is very insightful!
Vass
+2  A: 

The really nice thing about kbd is that what you type there is the same string that Emacs displays. So, try the following

C-h k M-<\delete>       (to use your syntax)

or

M-x describe-key M-<\delete>

Emacs (for me) responds with:

M-DEL (translated from <M-delete>) runs the command backward-kill-word, which is an interactive compiled Lisp function in `simple.el'.

It is bound to , M-DEL.

(backward-kill-word arg)

....

Which you can see shows that the representation for the key you want is M-DEL or M-delete.

Which is a long way of getting to the point that what you want is

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

Of course, if you have something in your .emacs that overrides it, the above won't help. You'll need to find that included library and stop using it (or customize its behavior).

Trey Jackson
Vass
I looked everywhere I could find emacs .el files and no where did I find a library...
Vass
A: 

Hello, After not being able to find the library holding the conflict I found this webpage http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Rebinding

Changing Key Bindings Interactively...

`M-x global-set-key KEY CMD ' Define KEY globally to run CMD....

Normally, C-z' is bound to the function suspend-emacs' (when not using the X Window System), but you can change C-z' to invoke an interactive subshell within Emacs, by binding it toshell' as follows:

 M-x global-set-key <RET> C-z shell <RET>

`global-set-key' reads the command name after the key. After you press the key, a message like this appears so that you can confirm that you are binding the key you want:

 Set key C-z to command:...

And now the standard default is returned to by doing

M-x global-set-key M-del ...

backward-kill-word But this is transient and must be done on each reload, any way to make this permanent? Putting a command into the init.el is not overriding the other effect

Vass
+5  A: 

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)
Gilles
(global-set-key [escape delete] 'backward-kill-word)put it into the init.el file in ~/.emacs.d/and it works!!!!
Vass
Just on thing now... using(global-set-key [escape delete] 'backward-kill-word)now trying other meta key commands like M-w or M-b get translated into escape w or escape b which are undefined... :_(
Vass
The solution for avoiding conflicts is to remap the faulty binding back to the correct one!!**(global-set-key (kbd "M-C-d") 'backward-kill-word)**
Vass
@Vass: `C-M-d` is a perfectly cromulent binding. The bug is that it's duplicated to `ESC delete`. But I did made a mistake in my workaround. I think `(global-set-key [?\e delete] 'backward-kill-word)` works on any version.
Gilles
ooh, wow, you were right!!! amazing, i didn't expect it. I thought that since your previous lines did a flush override that this wouldn't work either, but it did. I thought that you could only remap the binding in the init.el back to the desired default binding. Please modify your answer to include this (global-set-key [?\e delete] 'backward-kill-word) another me in the future begs for it.But why does this one work and not the previous ones you put? What's the essential difference?
Vass
@Vass: Oops, I missed a critical bit in my previous edit. In principle the answer lies in the “Keymaps” chapter of the Emacs Lisp manual, in particular the description of `function-key-map`. But it's a thorny corner case, and I can't figure out how to explain the results of my experiments from the manual.
Gilles