views:

897

answers:

4

How can I set a key binding that globally overrides and takes precedence over all other bindings for that key? I want to override all major/minor mode maps and make sure my binding is always in effect.

This of course doesn't work:

(global-set-key "\C-i" 'some-function)

It works in text-mode, but when I use lisp-mode, C-i is rebound to lisp-indent-line.

I can go through and override this binding in lisp-mode and in every other mode individually, but there must be an easier way. Every time I install a new mode for a new file type, I'd have to go back and check to make sure that all of my key bindings aren't being overridden by the new mode.

I want to do this because I want to emulate bindings I've already learned and ingrained from other editors.

A: 

I don't think you can. That is roughly equivalent to saying that you want to define a global variable that cannot be hidden by local variable declarations in functions. Scope just doesn't work that way.

However, there might be a way to write an elisp function to go through the mode list and reassign it in every single one for you.

T.E.D.
+17  A: 

I use a minor mode for all my "override" key bindings:

(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")

(define-key my-keys-minor-mode-map (kbd "C-i") 'some-function)

(define-minor-mode my-keys-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  t " my-keys" 'my-keys-minor-mode-map)

(my-keys-minor-mode 1)

This has the added benefit of being able to turn off all my modifications in one fell swoop (just disable the minor mode) in case someone else is driving the keyboard or if I need to see what a default key binding does.

Edit:

I forgot, you probably want to turn this off in the minibuffer:

(defun my-minibuffer-setup-hook ()
  (my-keys-minor-mode 0))

(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
scottfrazer
This seems like a good idea. Is there any way to make sure your minor mode doesn't fight with other minor modes?
Brian Carper
Make sure your minor mode is first on the list minor-mode-map-alist.
Trey Jackson
Trey is right. Usually putting this near the end of your .emacs is enough. Also, most bindings you'd override would be ones that major modes are setting ... minor modes generally stay out of the way.
scottfrazer
Thanks, I think this will work well.
Brian Carper
I followed this approach, but then I realized that anything I bind to C-i also gets bound to the TAB key. Any suggestions?
Steve
A: 

Unless you really want to do this yourself, you should check around and see if anyone else already has done it.

There is a package for Emacs which gives your windows-like keybindings. You should be able to find it through google.

JesperE
+1  A: 

Although scottfrazer's answer is exactly what you asked for, I will mention for posterity another solution.

From The Emacs Manual:

"Don't define C-c letter as a key in Lisp programs. Sequences consisting of C-c and a letter (either upper or lower case) are reserved for users; they are the only sequences reserved for users, so do not block them."

If you bind your personal global bindings to C-c plus a letter, then you "should" be safe. However, this is merely a convention, and any mode is still able to override your bindings.

Kirkland