bug
bug

Reputation: 565

How do I rebind a key in Emacs globally?

I have this in my .emacsrc

(define-key global-map "\C-h" 'backward-delete-char) ;;previously help

however, when I do an I-search and hit C-h to delete a char, emacs uses the default binding and tries to open the help, when I cancel the search with C-g, it then executes the backward-delete-char in the document I started the I-search from.

EDIT: This makes me wonder why C-h is again mapped to help inside the minibuffer. What is overriding my global-map definition and why?

Upvotes: 0

Views: 1149

Answers (2)

abo-abo
abo-abo

Reputation: 20352

You can use this:

(keyboard-translate ?\C-h ?\C-?)

This will translate C-h to backspace everywhere.

When you do isearch or ido-find-file, they override some keys in the minibuffer. You could re-override these keys for each mode, but you'd have to really do it for each new mode that you use. That's why I've suggested the code above: modes will not typically rebind backspace to something that doesn't behave like a backspace. The point is that the logical C-h is still bound for help inside minibuffer, but with keyboard-translate you don't have C-h anymore - you just have another backspace. And if you want to bind some command to it, you can't bind to C-h - you have to bind to ?\C-?.

Upvotes: 1

Stefan
Stefan

Reputation: 28561

My guess is that you don't really want to delete a char when you hit "control and h", but instead that for some reason Emacs receives C-h when you press the backspace key, right?

If so, the best solution is to figure out how to change your terminal emulator so that it doesn't send C-h to Emacs when you press backspace (e.g. it could send C-? as do most other terminal emulators nowadays).

Upvotes: 1

Related Questions