Asked  7 Months ago    Answers:  5   Viewed   52 times

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.

 Answers

71

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

(defvar my-keys-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-i") 'some-function)
    map)
  "my-keys-minor-mode keymap.")

(define-minor-mode my-keys-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  :init-value t
  :lighter " my-keys")

(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.

Note that you may need 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)
Tuesday, June 1, 2021
 
Pegues
answered 7 Months ago
38

The key binding you describe is defined in org.el like this:

(org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)

This means that it is only valid in org-mode-map, one of org-mode's local keymaps. The following code adds a hook that is run when org-mode starts. It simply removes that key binding from org-mode-map.

(add-hook 'org-mode-hook
          '(lambda ()
             (define-key org-mode-map [(control tab)] nil)))

Add this code to your .emacs file and then restart emacs.

Wednesday, August 11, 2021
 
Kasun Sandaruwan
answered 4 Months ago
70

The KeyBindings directory stores just per-user key bindings for already available operations in Xcode. On the other hand, the IDETextKeyBindingSet.plist lets you add an operation to Xcode itself (and afterwards put a key binding into your KeyBindings dir).

Just put the IDETextKeyBindingSet.plist file again into /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources (the location is the same as in Xcode 4). And make yourself a backup, since each Xcode upgrade will default your customized bindings (if there's a better solution for Xcode upgrades, I'm all ears).

Wednesday, August 18, 2021
 
rorymorris
answered 4 Months ago
25

See How to Use Key Bindings. Basically you bind a KeyStroke to an Action. The tutorial also has a section on How to Use Actions.

Wednesday, August 18, 2021
 
Seankala
answered 4 Months ago
46

The sequence you specify does simplify the keystroke representation. What you're left with is essentially the set of keystrokes you typed.

It's already been asked if you can convert an emacs macro into elisp.

Perhaps you're asking if it could be made more human readable? If so, then you're in luck.

You can use the kbd macro to convert the printed representation of keystrokes into the equivalent vector of keystrokes.

For example, the sequence which results in a query-replace of 3 with tj looks like: M-% 3 RET tj RET !

Well, you can manually set that up with:

(fset 'my-hand-crafted-kbd-macro (kbd "M-% 3 RET tj RET !"))

And this piece of elisp code should generate the above for you if you have named your macro 'my-hand-crafted-kbd-macro:

(defun insert-pretty-kbd-macro (macro-name)
  (interactive (list (intern (completing-read
                              "Insert kbd macro (name): "
                              obarray
                              (lambda (elt)
                                (and (fboundp elt)
                                     (or (stringp (symbol-function elt))
                                         (vectorp (symbol-function elt))
                                         (get elt 'kmacro))))
                              t))))
  (interactive)
  (insert "(fset '")
  (prin1 macro-name (current-buffer))
  (insert "n   (kbd "")
  (insert (key-description (symbol-function macro-name)))
  (insert ""))n"))

To get this to happen automatically when you finish a keyboard macro C-x ), you can use this advice:

(defadvice kmacro-end-macro (after kmacro-end-macro-name-it-and-insert activate)
  "user wants to insert elisp to reproduce the last kbd macro"
  (let ((name (read-string "Name for this kbd macro: ")))
    (insert "(fset '")
    (insert name)
    (insert "n   (kbd "")
    (insert (key-description last-kbd-macro))
    (insert ""))n")))
Thursday, August 19, 2021
 
Sam Adamsh
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share