What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 128 Stack Exchange communities.

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. –  kd304 Jul 20 '09 at 12:51
I think this is kind of dupe for this one: meta.stackexchange.com/questions/1191/… –  fretje Jul 20 '09 at 13:02
I think mine is a broader topic. –  kd304 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" –  kd304 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 –  kd304 Jul 20 '09 at 15:17

15 Answers 15

up vote 43 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<>? –  kd304 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: meta.stackexchange.com/q/157747/183189 –  MDeSchaepmeester Apr 26 '13 at 18:36

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

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

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

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

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      http://stackoverflow.com/
// @include        http://stackoverflow.com/questions/*
// @include        http://meta.stackexchange.com/questions/*
// ==/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? –  kd304 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 greasemetal.31tools.com, 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

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. –  kd304 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 requirement....now, 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: stackoverflow.com/questions/5038169/… –  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 at 17:00

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

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. –  kd304 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

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. –  kd304 Jul 14 '09 at 19:13

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. –  kd304 Jul 14 '09 at 19:23

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. –  kd304 Jul 14 '09 at 19:17

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. –  kd304 Jul 14 '09 at 19:19

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. –  kd304 Jul 14 '09 at 19:22

You must log in to answer this question.

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