2

The package HideShow (hs-minor-mode) defines some key bindings with prefix key C-c @.

What is the proper way to change this prefix key to C-c h?

The new key binding should also replace the old key bindings in the menu bar items of Hide/Show menu.

I came up with this solution for remapping:

(setcar (cadr (lookup-key hs-minor-mode-map (kbd "C-c"))) (elt (kbd "h") 0)))

It does what I want, but it looks wrong.

Note: I found this similar question. This Answer adds another Prefix but does not change the key binding shown in menu.

  • What do you mean by "menu", here? You say "show up in the menu" and "key binding shown in the menu". What menu are you talking about? – Drew Jun 21 '17 at 15:09
  • @Drew I updated this part of the question – jue Jun 21 '17 at 15:26
4

It depends on the minor-mode/package. Many make this more configurable, which doesn't seem to be true in this case. I think the simplest thing here would be to just redefine the keymap yourself instead of trying to fiddle with it. Try this

(setq hs-minor-mode-map
      (let ((map (make-sparse-keymap)))
        ;; These bindings roughly imitate those used by Outline mode.
        (define-key map (kbd "C-c h C-h") 'hs-hide-block)
        (define-key map (kbd "C-c h C-s") 'hs-show-block)
        (define-key map (kbd "C-c h M-h") 'hs-hide-all)
        (define-key map (kbd "C-c h M-s") 'hs-show-all)
        (define-key map (kbd "C-c h C-l") 'hs-hide-level)
        (define-key map (kbd "C-c h C-c") 'hs-toggle-hiding)
        (define-key map [(shift mouse-2)] 'hs-mouse-toggle-hiding)
        map))

Note: this alternative needs to be done before loading of hideshow.

I took that from the definition of hs-minor-mode-map which I found with C-h v. Since it is defined with a defvar you do not need to worry about your definition being overwritten when the package loads.

Update

Here's a shorter alternative

(define-key hs-minor-mode-map (kbd "C-c h") (lookup-key hs-minor-mode-map (kbd "C-c @")))
(define-key hs-minor-mode-map (kbd "C-c @") nil)

Note: this alternative needs to be done after hideshow has been loaded.

  • I do not want to redefine every single key binding. This seems not to be working? – jue Jun 21 '17 at 15:06
  • What menu?........... – Drew Jun 21 '17 at 15:09
  • @Drew the Hide/Show Menu from Menu Bar – jue Jun 21 '17 at 15:11
  • Well technically you do want to redefine all keybindings. As for the menu if you put this in your config before loading hideshow the menu will be correct. – justbur Jun 21 '17 at 15:13
  • Added another way to do this – justbur Jun 21 '17 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.