4

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
jue
  • 4,476
  • 8
  • 20
  • 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

1 Answers1

6

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.

justbur
  • 1,500
  • 8
  • 8