Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override/change mode key bindings in elisp?

Tags:

emacs

elisp

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

like image 516
Loren Avatar asked Jul 23 '11 20:07

Loren


2 Answers

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)))
like image 77
phils Avatar answered Oct 30 '22 07:10

phils


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)
    ))
like image 24
zev Avatar answered Oct 30 '22 05:10

zev