47

I have many classes and defs ...

I want to have + and - keys before class and def to collapse the class or open it ( toggle it ).

How i can do this?

5
  • Do you care about the functionality (code folding) or the +, - signs? Emacs is keyboard-based so it doesn't really make sense to use the mouse for toggling the +, -. Check 1085170 Commented Mar 9, 2011 at 12:35
  • no surly ... only i want see class is collapse and then open it and in other way Commented Mar 9, 2011 at 12:39
  • Good. See the link that I gave you; In the link there is a reference to the Emacs folding mode which might work for you. Commented Mar 9, 2011 at 12:45
  • Are you sure? Take a careful look in the python-related answer of 1085170 and the Ruby trick in the Emacs Wiki. Both ways look reasonable but you need to put some effort since python code is not wrapped using blocks like {}. Commented Mar 9, 2011 at 12:54
  • I haven't know that Emacs do code folding. This question is one of my favs! Commented Mar 9, 2011 at 13:26

2 Answers 2

52

Hideshow works out of the box and folds python code. It is built-in my version of emacs (24.3.1)

I have never needed more than these commands:

M-x hs-minor-mode
M-x hs-hide-all
M-x hs-show-all

To toggle use C-c @ C-c which probably needs rebinding. You might also want to setup a hook in your .emacs file for hs-minor-mode to automatically be enabled when opening .py files.

I use it in combination the following to jump around.

M-x imenu <my_func_name>
Sign up to request clarification or add additional context in comments.

6 Comments

This works great! But with a really large Python file (~5000 lines), things start lagging as I fold more classes. If I don't fold too many classes, it's okay. Specifically when I fold one class, ~1500 lines, it really slows down. I'm also using Elpy, which may be a factor.
5000 lines python file!? Is there a good reason for such a large file?
@cammil Maybe not, but I don't think the text editor is the right place to enforce that norm. If nothing else, I should be able to use code folding to help deal with managing the 5000 line file when I go to refactor it.
How can I print the folded code? ps-print-buffer exports unfolded always.
for all the other command related to hs-minor-mode
|
6

You can get code folding (and more) with CEDET. With CEDET, you should consider putting the following setting in your emacs configuration file:

(global-semantic-folding-mode t)

CEDET handles Python and other languages.

Other ideas about how you can make emacs even more convenient when programming can be found on StackOverflow.

1 Comment

CEDET seems to be included with Emacs 24, but this function isn't available. Has this changed in two years?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.