Currently, I use the hs-minor-mode to collapse my python code and make it easier to work with - however, by default this only folds def my_function(): blocks. Is it possible to get it to work with if, for, while etc. blocks as well?

If not, does anyone know a better option for code folding? I do use C-s and M-x occur to move around my code already, I just sometimes like to fold my code so I don't have so much to look at.


2 Answers 2


After a little more looking around, I found:

Which can do what I wanted (and possibly a little more). I haven't thoroughly tested them both out, but on first impression, yafolding is working the best so far on python.el.


I use spacemacs distro and was struggling with this issue. Use this gist to get hideshow folding to work with indentation.

Edit: Sure Drew, here's the elaboration.

  1. Add both the snippets found in the link to user-init() function of .spacemacs file
  2. Call hs-hide-level using M-x or keybind it, the way shown below
  (with-eval-after-load 'evil-maps
    (define-key evil-motion-state-map (kbd "zs") 'hs-hide-level))

right after the pasted snippets

  1. Profit :)
  • This is essentially a link-only answer, so it risks being deleted. Please summarize the answer that you invite readers to find at that link destination. Thx.
    – Drew
    Mar 16, 2021 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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