How to permanently enable the hs-minor-mode in emacs
Asked Answered
M

4

12

I am using thhs code in the .emacs file to permanently enable the hs-minor-mode and to change the shortcut:

(setq-default hs-minor-mode t)
(global-set-key (kbd "C-c C-h") (kbd "C-c @ C-h"))         ;;hiding block of code
(global-set-key (kbd "C-c C-r") (kbd "C-c @ C-s"))         ;;revealing block of code

But the mode is not activated automatically. what should i do?

Morpheus answered 6/10, 2012 at 20:27 Comment(0)
A
13

If you want it to be truly global, this does the trick:

(define-globalized-minor-mode global-hs-minor-mode
  hs-minor-mode hs-minor-mode)

(global-hs-minor-mode 1)
Asante answered 8/10, 2012 at 9:17 Comment(2)
Doesnt work for me. Emacs 24. Praveen's answer worked.Exceeding
I've tried stackoverflow.com/users/1123/magnar 's answer on Emacs 24.4.91.1, and found some buffer rejected to enable hs-minor-mode, because hs-minor-mode parse buffer to check availability. In my case, I could activate successfully with (defun my_hideshow-ignore-setup-failure() (ignore-errors (hs-minor-mode))) (define-globalized-minor-mode global-hs-minor-mode hs-minor-mode my_hideshow-ignore-setup-failure) Numberless
B
35

You can turn on hs-minor-mode for a specific mode like C, C++ mode using c-mode-common-hook.

(add-hook 'c-mode-common-hook #'hs-minor-mode)

In Emacs 24 or later, you can turn it on in all programming modes using prog-mode-hook.

(add-hook 'prog-mode-hook #'hs-minor-mode)
Bonner answered 6/10, 2012 at 21:3 Comment(5)
Still not working for some reason.I added the second code. I still have to switch on the hs-minor-mode.Morpheus
Before manually turning on, can you check the value of prog-mode-hook using C-h v (describe-variable) to see if what you have added is effective?Bonner
Two comments: (lambda () (hs-minor-mode)) is better written #'hs-minor-mode. And prog-mode-hook is new in Emacs-24 so if you use an older Emacs, changing that hook won't have any effect.Morris
(add-hook 'prog-mode-hook #'hs-minor-mode) works for me. I'm running Emacs 24.3.1Tartuffery
To add on to this I found it very helpful to bind toggle code block hiding to an easily used key (one of the function keys for example. Here is what i used in my .emacs file: (global-set-key (kbd "<f1>") 'hs-toggle-hiding)Pubes
A
13

If you want it to be truly global, this does the trick:

(define-globalized-minor-mode global-hs-minor-mode
  hs-minor-mode hs-minor-mode)

(global-hs-minor-mode 1)
Asante answered 8/10, 2012 at 9:17 Comment(2)
Doesnt work for me. Emacs 24. Praveen's answer worked.Exceeding
I've tried stackoverflow.com/users/1123/magnar 's answer on Emacs 24.4.91.1, and found some buffer rejected to enable hs-minor-mode, because hs-minor-mode parse buffer to check availability. In my case, I could activate successfully with (defun my_hideshow-ignore-setup-failure() (ignore-errors (hs-minor-mode))) (define-globalized-minor-mode global-hs-minor-mode hs-minor-mode my_hideshow-ignore-setup-failure) Numberless
P
1

If you want to enable it everywhere, and start the buffer with the code folded by hs-hide-all, do

(defun my-hide-all()
  (interactive)
  (hs-minor-mode)
  (hs-hide-all))
(add-hook 'prog-mode-hook 'my-hide-all)
Pseudohermaphrodite answered 31/5, 2017 at 21:51 Comment(1)
Just a heads up, this will cause hs-minor-mode to try and turn on in modes that don't like it. Like fundamental, or minibuffer, for instance.Semipostal
Y
0

I tried the previous answers, but the one below works best for me.

(add-hook 'vhdl-mode-hook #'vhdl-hs-minor-mode)
Yielding answered 31/3, 2023 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.