Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 158 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

It would be great to have the option to fold code snippets, entire questions and answer posts on a user basis and allow defining some generic foldable regions within the question/answer editor.

Non generic folding info could be stored in cookies perhaps to move the storage/processing requirement to the client side.

Definable folding can take any free and safe HTML tag or other multi character trick (<>Fold this<>)

share|improve this question
Usually I wouldn't call a bounty onto a feature request, but my question is getting old, and I'd like some response from the powers that be. As all answers are equally supporting, I'll award the bounty to that person, who can make it use the best. – akarnokd Jul 20 '09 at 12:51
I think this is kind of dupe for this one:… – fretje Jul 20 '09 at 13:02
I think mine is a broader topic. – akarnokd Jul 20 '09 at 13:14
I think the other one is broader, as that one is about a tag for hiding any part of your question/answer. Yours is only about hiding code, so narrower (is that the right word?) in my opinion. – fretje Jul 20 '09 at 14:43
Quote: "allow defining some generic foldable regions" – akarnokd Jul 20 '09 at 15:00
Okay, got it... not so clear when looking at the title though... and I still think they fall into the same bin... – fretje Jul 20 '09 at 15:10
You weren't there at the beginning I guess. Rich B's edit changed my original title: "Code, block, question and answer folding" - still visible in history – akarnokd Jul 20 '09 at 15:17

18 Answers 18

up vote 67 down vote accepted

Yes, I like this idea a lot. Every so often I post an answer where the main bit is actually quite small, but a footnote has a lot of code baggage - it would be great if that baggage could be folded up to keep it in proportion with respect to the rest of the post.

share|improve this answer
Hmm, you imply the user should be able to define the default folding status on his region? ><Unfolded by default><, <>Folded by default<>? – akarnokd Jul 14 '09 at 19:21
Yes, quite possibly. – Jon Skeet Jul 21 '09 at 9:08
This is a great reason for folding. Why did the powers that own Stack Overflow reject this? – Vaccano Jan 28 '10 at 4:00
Is there a chance to reconsider this? Please see my question as well: – MDeSchaepmeester Apr 26 '13 at 18:36

I like the idea, especially because it would make it easier to make a question 'multi lingual' where you could post a question or answer with multiple code samples in multiple languages without making it a nightmare to read.

share|improve this answer
Like how MSDN does their code samples in C#, VB, and C++. – TheTXI Jul 14 '09 at 14:42
@TheTXI: Exactly. – GEOCHET Jul 14 '09 at 15:15
For language agnostic questions, nice. – akarnokd Jul 14 '09 at 19:23
up vote 24 down vote

This feature request (and it being declined) is almost 6 years old. I'd appreciate if this could be reconsidered. There are a lot of use cases that have been mentioned but I don't even think this should be focused on code blocks. Since code blocks get a scrollbar anyway you can already fill your 30k character limit with a single code block, without taking up a lot of vertical space. So putting in massive amounts of code isn't really discouraged by the system to begin with.

However, I think collapsible regions would be much more useful for normal explanations. In many cases there is the simple answer which will serve most people's needs, and then there's also the in-depth "if you really want to know the details" explanation. In those cases, I would prefer not to take up so much screen real estate for the people who are not interested in the full explanation, but I'd also prefer not to link to the gritty details externally.

You might say "a good answer is tailored to its audience, just pick the short or the long version appropriately" - but I'm not writing the answer only for the OP but also for future visitors, so catering to both cases would certainly improve the usefulness of the answer.

I also don't understand why some people are so strongly opposed to this: collapsible regions are already a thing on SE. On one hand, Stack Snippets can be hidden, but more importantly spoilers are implemented on mobile as collapsible regions. If they're such a bad thing, why have them on one platform in the first place? Couldn't we just turn spoilers into collapsible regions everywhere? (Although a comment-based syntax like Stack Snippets would be more convenient than prepending >! to every single line.)

share|improve this answer

Personally, I like the idea. I've been guilty of long-freakin'-code-posting myself. However, I somehow doubt Jeff would agree. :)

share|improve this answer
Jeff can opt-out of such feature and he won't experience it at all. – akarnokd Jul 14 '09 at 19:12
Jeff's arguments are quite valid in the context of a software development environment. StackOverflow, however, is not a software development environment, not an IDE. It is a place to talk about problems with source code. Sometimes you need to share the code so that someone can run it -- they need not examine the entire snippet. There are text pastebins that exist, and can be linked to from within a question, however it makes more sense to fold the code and keep the code bundled with the question on StackOverflow. This would then allow for personalized fold/unfold settings. – Dave Jarvis Dec 8 '10 at 15:38
Jeff was probably sick of getting blamed for his code :v – kekekela Nov 14 '11 at 1:38
Which bit of that article are you referencing? I'd rather not read more than I have to of things written by Jeff. :-/ – James Haigh Mar 10 '15 at 22:00
Oh wait, it's a broken link. Here's an Internet Archive Wayback Machine capture of the article titled “The Problem With Code Folding”, dated 2008-07-06:… You see, that's the problem with external links, especially in the case of pastebins which may be set to expire. – James Haigh Mar 10 '15 at 22:46
(I was initially just seeing the unrelated articles at because that's what the broken link silently 301-redirected to without me noticing, hence why it wasn't obvious as to what you were referring.) – James Haigh Mar 10 '15 at 22:59
Updated link with wayback link provided by @James Haigh. Thank you. – John Rudy Mar 16 '15 at 15:48

This is a hugely helpful idea for the [R] tag and I suspect any other statistics/data-heavy tags as well.

We strongly recommend our users include sample data:

How to make a great R reproducible example?

Often due to the nature of the statistical beast even carefully culled sample data is unwieldy. For instance this well-conceived post:

Dealing with Messy Dates

There's simply no way to include the variety of examples the questioner was interested in without a huge code dump. I tend to put mine at the end, but it's still a sub-optimal solution that makes answering harder and the site less beautiful.

share|improve this answer
I agree with the idea, but that dump of dates is breathtaking. Not all train wrecks should be hidden, and that is a very memorable train wreck. Still, you're right. Some people seem to post their stuff to pastebin or other sites, in order to have the effect of code folding. – Iterator Nov 14 '11 at 13:11

Here is my solution to this problem - a greasemonkey script which will hide all but the first line of each code block until it is clicked on (click again to re-fold the block). It is a bit hackish atm, but it does work on my computer. If you find any bugs in it, let me know and I will try to fix them.

EDIT: Added menu commands to show and hide all foldable blocks.

// ==UserScript==
// @name           Code folding
// @namespace
// @include*
// @include*
// ==/UserScript==

//default values

// Add a selector for each element that you wish to fold
// to the array below (one array item per selector
var elementsToFold = new Array("pre:has(code)");

// Close elements by default?
var closeByDefault = true;

//- Do not edit below this line -
function getJQuery() {
    if (typeof unsafeWindow.jQuery == 'undefined') {
        window.setTimeOut( getJQuery, 100 );
    else {
        $ = unsafeWindow.jQuery; letsGo();
//add the folded style into the document
var style = window.document.createElement("style");
style.type = "text/css";
style.innerHTML = ".folded {height:1.0em; overflow:hidden;cursor:N-resize;}";


function letsGo () {
    $(elementsToFold.join(",")).click( function() {
    window.showHideElements = function (event) {
    window.hideAllElements = function () {
        $(elementsToFold.join(",")).each(function() {
    window.showAllElements = function () {
        $(elementsToFold.join(",")).each(function() {

    if (closeByDefault) {
        $(elementsToFold.join(",")).each(function() {

    GM_registerMenuCommand("Show all folded blocks", showAllElements);
    GM_registerMenuCommand("Hide all foldable blocks", hideAllElements);
share|improve this answer
Can I make this somehow work in Chrome? – akarnokd Jul 22 '09 at 6:40
Apparently Chrome has experimental support for greasemonkey scripts - start with chrome --enable-greasemonkey. The other option is to try, which apparently supports most grease-monkey scripts. However, I don't use chrome, so I can't be sure. If you have more information, let me know and I will update my answer accordingly. – a_m0d Jul 22 '09 at 7:00

This would be very nice - especially if it's closed (or showing only the first line of code) by default.

share|improve this answer
First line is nice and you can just have a // short description in it. – akarnokd Jul 14 '09 at 19:22

I like the idea of automagically inserting folding on (at least) code. It would help with the newbies who dump their entire architecture into a question -- which I still prefer over those who provide no code at all. Anything over, say, 10 lines? What's the cutoff for when it adds a scroll bar? Because that's probably about the time we should definitely have a fold.

share|improve this answer
I don't see any trouble having a highly customizable folding settings. jQuery is a powerful library. – akarnokd Jul 14 '09 at 19:19

It would also be useful to support collapsed sections of text or images. Example usages:

  • On one of the mathematical Stack Exchange sites, I might write an answer which contains a long but relatively uninteresting proof of one of the claims. Since the proof is needed for completeness but may be a detour from the main point, I want it to be collapsed until an interested user clicks.
  • For a question on a graphical computer program, I might include a screenshot illustrating what I want to do. I might have some additional screenshots illustrating more examples; these would probably only be interesting to someone that is trying to answer my question.
  • Details on multiple options. For example, a User Experience question might ask:

Which position for some control would allow the fastest navigation by users?

  • Controls on the left
    (Collapsed: UI mockup with controls on the left)
  • Controls on top
    (Collapsed: UI mockup with controls on top)
  • Controls on bottom
    (Collapsed: UI mockup with controls on bottom)
share|improve this answer

I’d like to provide (but one) example of a valid reason for providing collapsible elements: log files. Sometimes you must provide logs for analysis, and often they tend to be lengthy and cannot be pared down without losing potentially valuable information. Yet, there is no reason to have them be full-length by default. For example, someone may be able to diagnose an issue without even looking at it while someone may need some clue from the log to figure out the problem.

share|improve this answer

Great idea. I'd suggest supporting it for quote blocks as well.

share|improve this answer
That's implicit if you wrap your quoted text in a foldable region. – akarnokd Jul 14 '09 at 19:13

How about now?

With the advent of live examples, it would be nice to also keep the code onsite on the off-chance the live example site goes offline or loses the contents, but also nice to keep the code collapsed (as it's already readily available, and live examples tend to accommodate more code).

Pretty please?

share|improve this answer

They could rig up something like this, they already use these interface elements elsewhere in the site:

Code Block Dropdown

So of course, hitting the arrow would bring up the full code block. I'd actually do it somewhat differently in order to not lose width on the code block, but something along these lines would be helpful.

share|improve this answer

This is a good idea, especially for code. I sometimes find myself putting a sizable piece of sample code into an answer, which some people may just not want to read through. It would be nice if I could fold it for people to skip over if they like.

Incidentally, this would also help with "code golf" or challenge questions where solutions or hints included with the question could be folded so that people don't have to see them if they don't yet want to.

share|improve this answer
It depends on what you consider the default state of foldable objects. – akarnokd Jul 14 '09 at 19:17

I'd like to see the code block small at first, with an option to expand beyond the bounds of the question (to solve the not-too-uncommon problem of horizontal scrolling).

share|improve this answer

You mean a collapsed code-snippet like this?

alert('Most folded code ever!');

share|improve this answer
Exactly. But does this work with all languages, or even pseudo-code? – Devil's Advocate Dec 18 '15 at 16:19
@ScottBeeson infuriatingly not. This issue feels like the US-campaign-finance reform of Stackoverflow; 80% or so seem to want it, but those with decision-making power prefer to keep doing other things. – SeldomNeedy Dec 18 '15 at 18:58

I think one of the real issues here is a lack of control over how the feature could be used. Users unfamiliar with SE could do all sorts of thing with a feature like this, potentially creating time consuming issues for users that are moderating.

In addition to that, it is well known that Google prefers that you hide as little content as possible. It would be necessary to inform users of this SEO practice, before one could reasonably ask them to use it correctly.

I personally like the idea, but there are significant practical issues worth considering.

share|improve this answer

Line numbers and code folding don't belong here, because you shouldn't be posting huge swaths of code anyway. If it's more than a few snippets, the question and/or the answers are busted.

If you "need" to link to a lot of code, host it elsewhere, and give us the relevant snippet with a link to the rest.

Any features that encourage posting a lot of code are also broken by design and will be declined for the same reason -- why would we want to encourage behaviors that lead to unreadable posts?

share|improve this answer
I could argue, but I simply accept your view. – akarnokd Jul 21 '09 at 8:55
It's quite possible to have long answers that include several sections, some of which are almost incidental. Being able to fold those sections up would make the answer easier to navigate. There are more reasons for wanting folding than just "there's one huge block of code." – Jon Skeet Jul 21 '09 at 9:09
half the time I'd rather fold up the text and just look at the code. – Steven A. Lowe Jul 21 '09 at 14:33
@Jeff: Please think about it from a futuristic point of view. Hosting the code somewhere else and linking to it means that when that site goes down, we end up with a stale link and probably a stale answer here. This kind of splitting up of the answer into two sites means two points of failure. And "huge swaths of code" are unavoidable in some cases to provide even simple and legitimate examples - mostly due to verbose languages, but also due to other reasons. – sundar Jul 22 '09 at 7:00
I also disagree with you Jeff....I don't see any reason why some legitimate questions wouldn't benefit from, or actually require, large amounts of supporting code. Let's say someone is asking a question about how to handle a fairly in depth, if I've done something similar, I may be willing to post entire code files as it takes just a few minutes, but, to work through it all and extract only the relevant bits and pieces and post them, and hope I haven't missed anything crucial, well, I may not bother. Hopefully you reconsider this (folding AND uploading). – tbone Aug 21 '09 at 20:31
I would like to point out some users are downvote and vote-to-close-as-spam happy if you link to pastebins for large amounts of code. – Macha Oct 17 '09 at 13:08
@Jeff; you need to define "huge swaths". I have had several questions which need code which is not really relevant simply because if I don't post it I know that the first comments gonna be "show us the code". – Lawrence Dol Dec 23 '10 at 0:04
@Jeff: Here is an example of a good answer which would be better if the code were folded by default:… – Lawrence Dol Feb 20 '11 at 21:37
@Jeff: Also, you need to reconsider your decision to decline, I think, based on the vote pattern in this question, assuming that the votes represent the community. – Lawrence Dol Feb 20 '11 at 21:38
@Jeff - You may be interested in this question, which was created when someone posted an answer about how to link a DLL in several ways here. My answer was "Scrollbars appear at 25 lines, and you get 30k characters - Use them!" Did you have the scrollbars at the time this question was asked? If not, I think it might be good to revisit it. – Kevin Vermeer Nov 14 '11 at 1:41
@Jeff In practice I've seen that others follow this suggestion, by way of pastebin. – Iterator Nov 14 '11 at 13:16
@Jeff, just because you can’t think of a valid reason for it doesn’t mean there isn’t. Consider log files. Sometimes you must provide logs for analysis, and often they tend to be lengthy and cannot be pared down without losing potentially valuable information. Yet, there is no reason to have them be full-length by default. For example, someone may be able to diagnose an issue without even looking at it while others may need some clue from the log to figure out the problem. – Synetech Apr 21 '14 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .