Globally override key binding in Emacs

后端 未结 8 2139
栀梦
栀梦 2020-11-22 06:51

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

8条回答
  •  隐瞒了意图╮
    2020-11-22 07:49

    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)
    

提交回复
热议问题