Emacs: unbind all keys of some-mode-map
Asked Answered
E

1

5

I know, that I can unbind keys using unbind-key. In this case I have to know the key I want to unbind. But I don't know the key. I just want to unbind all keys of some-mode-map. I don't want override them, I just want to unbind them and then to define my own keys in the some-mode-map. I know that I could iterate some-mode-map and apply unbind-key, but may be there is a more straightforward solution? And I don't know how to iterate a keymap.

Update

Tried (as was suggested by phils in comments) this way and it didn't work:

(use-package neotree
  :config
  (setq neotree-mode-map (make-sparse-keymap))
  (define-key neotree-mode-map (kbd "RET") 'neotree-enter)
)
(use-package evil
  :init
  (setq evil-overriding-maps '((neotree-mode-map)))
)

neotree-mode-map remains not cleaned. And depending on package configuration order, evil bindings gets overridden. It doesn't unless I redefine neotree-mode-map. I know I could obtain desired result with evil-define-key, but I want to keep package specific settings in its own configuration file. If I employ evil-define-key, neotree key binding definitions will be in setup-evil.el and not in setup-neotree.el

Update 2

Answer works, as does (setq neotree-mode-map (make-sparse-keymap)). But it appears that neotree-mode-map has parent map, and that's why some key bindings "come back" after "cleaning". Another thing I figured out is, that evil put into overriding maps some extra information.

(keymap 
... 
(override-state . all) 
...
)

And that's why I should clear map before evilconfiguration.

Execratory answered 20/8, 2017 at 12:39 Comment(3)
A near-duplicate of emacs.stackexchange.com/questions/32389/…Torr
Are you sure that neotree-mode-map "remains uncleaned"? Or is it that you have correctly redefined that variable but existing code still points, not to the variable, but to its former value (a keymap, i.e., a list)? What does C-h v neotree-mode-map tell you about this? (Or use C-h M-k (describe-keymap) from library help-fns+.el, for a human-readable listing of the keymap bindings.)Misrepresent
neotree-mode-map value is: Value: (keymap (override-state . all) (13 . neotree-enter) keymap (103 . revert-buffer) (60 . beginning-of-buffer) (62 . end-of-buffer) (104 . describe-mode) (63 . describe-mode) (127 . scroll-down-command) (33554464 . scroll-down-command) ... (45 . negative-argument) (remap keymap (self-insert-command . undefined))) Execratory
D
6

You can gut all the bindings in a keymap with the following:

(setf (cdr <keymap>) nil)

I think in practice it rarely does really what you want, because there'll be some bindings you didn't realize were useful (e.g. those corresponding to menubar entries), but you get what you asked for.

As for iterating a keymap, you can use map-keymap for that.

Deontology answered 25/8, 2017 at 12:12 Comment(1)
(setcdr <keymap> nil) is the shorter version.Exclamatory

© 2022 - 2024 — McMap. All rights reserved.