How to override / change mode key bindings in elisp?

In particular, when I load dired-x, it sets Mo to switch the secondary mode. I am using Mo for another window, so I would like to change the key that dired-x associates with something else. I tried to set the key after the mode loads as follows:

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

but to no avail.

+5
source share
2 answers

Slightly better than adding another copy of your custom global anchor to the local mode map, it will remove the local anchor so that it no longer distorts the global anchor. You can also give this function a new key before doing 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)))
+7
source

Dired-mode "" , " -" . , , dired-mode:

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

All Articles