开发者

How to override/change mode key bindings in elisp?

开发者 https://www.devze.com 2023-03-22 06:21 出处:网络
In particular, when I load dired-x, it sets M-o to toggle the omit minor mode. I use M-o for other-window, so I would like to change the key that dired-x b开发者_高级运维inds to something else. I\'ve

In particular, when I load dired-x, it sets M-o to toggle the omit minor mode. I use M-o for other-window, so I would like to change the key that dired-x b开发者_高级运维inds to something else. I've attempted setting the key after the mode loads like this:

(add-hook 'dired-mode-hook
  (lambda ()
    (dired-omit-mode 1)
    (global-set-key (kbd "M-o") 'other-window)
    ))

but to no avail.


Slightly better than adding another copy of your custom global binding to the local mode map, would be removing the local binding so that it no longer shadows the global binding. You might also give that function a new key before you do this.

(eval-after-load "dired-x"
  '(progn
     ;; Add an alternative local binding for the command
     ;; bound to M-o
     (define-key dired-mode-map (kbd "C-c o")
       (lookup-key dired-mode-map (kbd "M-o")))
     ;; Unbind M-o from the local keymap
     (define-key dired-mode-map (kbd "M-o") nil)))


The dired-mode bindings "shadow" the global ones so your "global-set-key" isn't helping. What you want to do is override the dired-mode binding:

(add-hook 'dired-mode-hook
  (lambda ()
    (dired-omit-mode 1)
    (define-key dired-mode-map (kbd "M-o") 'other-window)
    ))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号