There are several questions on SO about how to get code folding in emacs, without having to add any special characters like "markers" in the comments for example. Someone said that there was "no perfect solution."
It seems that it could be done by parsing the source of the program being written and look for matching parenthesis or bracket, or to do it based on indentation. You could also use a combination of scripts that use different methods.
So why is it commonly accepted that there is no "perfect" and straightforward way to get code-folding in emac? Is there something in emacs or its architecture that makes it hard to program? If it were easy, after so many years of smart people using emacs you would think that someone would have wrote it.

- 9,819
- 4
- 38
- 67

- 519
- 1
- 7
- 19
-
7because "real programmers" don't need code folding! – Mar 08 '10 at 06:23
-
5In fact, it's so easy, everyone wrote their own. – huaiyuan Mar 08 '10 at 07:11
-
See also: http://stackoverflow.com/questions/382781/emacs-equivalent-of-vims-foldmethod-indent/448724 – Michael Paulukonis Mar 08 '10 at 15:00
-
5@fuzzy "real programmers" use a magnetized needle and a steady hand. (http://xkcd.com/378/) – cobbal Mar 08 '10 at 21:17
-
1A good example of the lisp curse. (http://www.winestockwebdesign.com/Essays/Lisp_Curse.html) – R. P. Dillon Dec 26 '11 at 08:08
-
3@JarrodRoberson - surely it should be that "real programmers" use `narrow-to-region` ;) – ocodo Jul 24 '15 at 01:55
-
1@ocodo `narrow-to-region` is a much better idea! – lkahtz Mar 03 '21 at 02:01
7 Answers
You should play with Hideshow (hs-minor-mode
) combined with fold-dwim.el
. It does exactly what you suggested -- looks for matching braces/parens, and can be set up to fall back on the indentation.
There's a robust folding solution out there for most common languages, and if there isn't, all the folding packages are highly customizable. In fact, the only downside is the proliferation of folding methods (fold-dwim
helps quite a bit with that); I used to think that because nobody could point me to a definitive solution, folding was hard or impossible — in fact, the opposite is true. You just have to experiment a little to see what works best for you.
I have used folding.el
(e.g. to group stuff in my .emacs
), outline-minor-mode
, and now Hideshow. There's some chance that none of them would work exactly the way you want right out of the box (e.g. you might need to set up an outline regex, or define folding marks for folding.el
), but it turns out to be easy. The default keybindings can be somewhat baroque, but this is remedied by fold-dwim
and/or hideshow-org
(highly recommended for Hideshow, cf the Emacswiki hideshow page; you can also mimic hideshow-org
's behavior for other folding modes with some quick-and-dirty elisp and fold-dwim
). Once you figure out your preferred setup, just turn it on automatically via hooks or buffer-local variables, and watch your code fold away :)

- 27,060
- 21
- 118
- 148

- 12,893
- 5
- 44
- 44
-
3AFAIK, there is no perfect hs-minor-mode setting even for popular languages such as python and lua for reasons. Also the fallback on the indentation (= selective-display) is a little too primitive compared to other editors such as vim. I am an emacs user for many years and have been using all of your suggestions, but I agree with the original poster. – tbear May 31 '11 at 01:10
-
1[hideshowvis.el](http://www.emacswiki.org/emacs/download/hideshowvis.el) is also worth looking at. – Mark Feb 21 '13 at 20:51
-
1`hs-minor-mode` and `vimrc-mode` together work pretty well with `vim`'s standard folding, `{{{`, `}}}`. – Brady Trainor Jul 13 '14 at 08:44
You should look into CEDET. It does code-folding just fine, and many other fancy features that you're probably looking for if you're switching from an IDE to Emacs.
Specifically, look for `global-semantic-tag-folding-mode'

- 797
- 1
- 7
- 6
You don't need anything extra, just enable outline-minor-mode for file types you want to fold.

- 49,047
- 44
- 147
- 241
-
2i would have thought that outline-minor-mode would be the accepted answer instead of cedet, since it comes inbuilt with emacs. (http://mytechrants.wordpress.com/2009/01/05/emacs-tip-of-the-day-collapsing-functions/) – vedang Mar 09 '10 at 04:25
-
1Very cool - I didn't know about this. I was looking for a way to fold-down headings in markdown-mode ala org-mode. – jkp Sep 20 '11 at 09:32
-
Outline mode is for headings, that's not necessarily suitable for code editting : usually, code blocks are opened and closed, and there may be some stuff between blocks. That's not something you can achieve with a headings-based solution. – T. Verron Oct 29 '13 at 12:41
-
I like to combine this with `outshine.el` package. Though I've only used it for my `.emacs` so far. – Brady Trainor Jul 13 '14 at 08:42
But in fact, there ARE various solutions for Emacs; I have listed some of them (those I have happened to come across) at http://en.wikipedia.org/w/index.php?title=Code_folding&oldid=375300945#cite_note-2.
Though, there are things I'm missing: in some cases, I'd like to combine several mechanisms: for example, for markdown, I'd like to use outline-based folding (for sections) and indentation-based folding (for quotations, code blocks etc.) -- in order not bother with implementing a complete parser for markdown.
Here they are:
Token-based folding in Emacs
Token-based folding in Emacs is impemented by the folding minor mode.
Indentation-based folding in Emacs
One can use the set-selective-display
function in Emacs to hide lines based on the indentation level, as suggested in the Universal code folding note.
Syntax-dependent folding in Emacs
Syntax-dependent folding in Emacs is supported by:
the outline and allout modes for special dedicated "outline"-syntaxes;
by the hideshow minor mode for some programming languages;
also,
- by the semantic-tag-folding minor mode and the
senator-fold-tag
command for syntaxes supported by semantic, - as well as by doc-mode for JavaDoc or Doxygen comments,
- by TeX-fold-mode
sgml-fold-element
command,- nxml-outln library in the corresponding language-specific modes, and possibly in other modes for particular syntaxes.
- by the semantic-tag-folding minor mode and the
Several folding mechanisms are unified by the fold-dwim interface.
See also http://www.emacswiki.org/emacs/CategoryHideStuff.
Folding of user-selected regions in Emacs
Folding of user-selected regions in Emacs is implemented by the hide-region-hide command.

- 4,921
- 6
- 53
- 104
I have been using folding-mode for quite some time. With auto-insert template and abrevs it works quite well for me for for some nice bricks of code.
Being able to produce the buffer folded (for printing/emailing) has always been a desire of mine. Some of my folding tags are for secure / password hiding.

- 1,336
- 7
- 7
I know this is a bit old but for me origami.el works perfectly well out of the box.

- 497
- 3
- 13
Yes Finally code folding is there in emacs. Try yafolding present at melpa.org package library.

- 119
- 3