45

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?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Mohammad Efazati
  • 4,812
  • 2
  • 35
  • 50
  • 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](http://stackoverflow.com/questions/1085170/how-to-achieve-code-folding-effects-in-emacs) – sakisk Mar 09 '11 at 12:35
  • no surly ... only i want see class is collapse and then open it and in other way – Mohammad Efazati Mar 09 '11 at 12:39
  • Good. See the link that I gave you; In the link there is a reference to the [Emacs folding mode](http://www.emacswiki.org/emacs/FoldingMode) which might work for you. – sakisk Mar 09 '11 at 12:45
  • Are you sure? Take a careful look in the python-related answer of [1085170](http://stackoverflow.com/questions/1085170/how-to-achieve-code-folding-effects-in-emacs/1085551#1085551) and the [Ruby](http://www.emacswiki.org/emacs/FoldingMode#toc3) 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 {}. – sakisk Mar 09 '11 at 12:54
  • I haven't know that Emacs do code folding. This question is one of my favs! – swdev Mar 09 '11 at 13:26

2 Answers2

51

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>
Greg
  • 1,894
  • 18
  • 11
  • 1
    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. – modulitos Oct 27 '15 at 10:59
  • 8
    5000 lines python file!? Is there a good reason for such a large file? – cammil Feb 28 '17 at 15:27
  • 2
    @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. –  May 16 '18 at 18:37
  • How can I print the folded code? `ps-print-buffer` exports unfolded always. – Torsten Bronger May 24 '18 at 06:05
  • for all the other command related to [hs-minor-mode](https://www.gnu.org/software/emacs/manual/html_node/emacs/Hideshow.html) – Arun Kumar Khattri Apr 02 '20 at 13:42
  • Note that for Python, Hideshow can only fold entire functions. It can't do arbitrary scopes (like a loop or a conditional) – John de Largentaye Jul 18 '23 at 17:28
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.

Community
  • 1
  • 1
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260