I use hs-minor-mode currently.

When point is on class Foo... a M-x hs-hide-level will fold all methods of that class.

This is exactly the state I need for all *.py files when I open them. Is it possible?

When I open a python file all methods of all classes (there could be more then one!) should be folded.

hs-hide-level was pointed out in that answer. https://emacs.stackexchange.com/a/50144/12999

1 Answer 1


Here's a quick idea:

(defun my-collapse ()
  (when (and (stringp buffer-file-name)
             (string-match "\\.py\\'" buffer-file-name))
      (goto-char (point-min))
      (while (re-search-forward "class" (point-max) t)
        (hs-hide-level 1)))))

This checks that the file being visited ends with ".py". It then saves where you are in the buffer before searching for the word "class". At each match, it calls the function to 'collapse' items in that block whose indent level is greater than or equal to 1. This should be all methods, assuming the class is not nested. Your point is returned to where it was prior to the search.

To run it on every .py file, use a hook:

(add-hook 'find-file-hook 'my-collapse)

Your Answer

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

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