简体   繁体   English

如何在elisp中覆盖/更改模式键绑定?

[英]How to override/change mode key bindings in elisp?

In particular, when I load dired-x, it sets Mo to toggle the omit minor mode. 特别是,当我加载dired-x时,它设置Mo以切换省略的次要模式。 I use Mo for other-window, so I would like to change the key that dired-x binds to something else. 我使用Mo作为其他窗口,所以我想更改dired-x绑定到其他东西的键。 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: 你想要做的是覆盖dired-mode绑定:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM