Possible Duplicate:
How do I get my code blocks formatted?

where should we post large chunks of code that we don't want to clog up our posts?

Is there a way to make a scrollable code block? Or do we have to use external website like pastebin?

Second question - is there a more reliable way to paste code blocks? I'm finding it difficult to paste code on SO sometimes.. it will sometimes only take the 'middle chunk' of my code block as code, no matter what I do or how hard I indent or where i put the < code > blocks... any advise??


4 Answers 4


enter image description here

  • 3
    +1 for the work that must have took Commented Jan 26, 2010 at 0:43
  • 1
    +1 for the effort, although the site of that thing makes me want to poor H2SO4 into every orifice of my body
    – squillman
    Commented Jan 26, 2010 at 2:06
  • 4
    aaaaaaaaggghhhhh!!! kill it! killitkillitkillitkillit!!! Commented Jan 26, 2010 at 2:42
  • @squillman. Were the misspellings site and poor deliberate?
    – TRiG
    Commented Feb 4, 2013 at 12:49

Regarding scrolling code blocks:

If you need that much space for the code on which you're asking a question, you probably have a larger problem. Generally speaking, if I have to read your code for 5 or 10 or 20 minutes to understand what's going on, I'm not going to answer your question. It's essentially the "big wall of text" problem, but with syntax highlighting.

My recommendation is to find the smallest working set of code possible which reproduces the error, and paste that in instead. This helps those of us who might want to answer by giving us only what we need (better signal-to-noise ratio), and it helps you by not (potentially) exposing more of your (or your company's) code than is critically needed.

  • I have an issue with SqlCacheDepedency. I found someone willing to help me on SO so I wanted to post the SQL Trace as things happened on my website. But even posting a 50 line snippet of SQL logs is ugly and harsh. There ARE legitimate uses for a code paste / scrollable code text box!
    – user142370
    Commented Jan 25, 2010 at 23:01
  • 4
    post it as a link from pastebin Commented Jan 25, 2010 at 23:52
  • 1
    @unknown: You gotta go with the guy who runs the site. :)
    – John Rudy
    Commented Jan 26, 2010 at 1:32

After you paste the code block, select it, then hit the code button:

  • thanks! never saw or realized what that button did.
    – user142370
    Commented Jan 25, 2010 at 23:03

Very large code blocks automatically scroll both horizontally and vertically. They still take up a lot of vertical space before they scroll, though, so your post may still appear pretty cluttered.

In general I've suggested pasting only tiny excerpts of code, with links to pastebin or similar services where the larger pieces of code exist to put the small post into context.

Regarding your second question, Code blocks must be indented by 4 spaces on every line. The editing bar has a code icon (looks like a small block of 1's and 0's) to help with this. Paste your code, then select it in the editing box, and press the code icon. It'll indent everything you've selected by 4 spaces, and it will show up correctly.

Unfortunately using <code> and other HTML tags may fail due to the use of < and > symbols in the code, so the best way is to indent everything 4 spaces.

  • Vertically? Are you sure? I've never seen a vertical scroll bar except when there's also a horizontal scroll bar.
    – mmyers
    Commented Jan 25, 2010 at 23:21
  • 2
    @mmyers You ought to frequent the C++ tags more often - see stackoverflow.com/questions/2135355/…
    – nb69307
    Commented Jan 25, 2010 at 23:57
  • Huh, I wonder why the downvote?
    – Pollyanna
    Commented Jan 26, 2010 at 0:27
  • @Neil: I'm not sure what that's supposed to prove. I don't see any scroll bars on that question or any of the answers.
    – mmyers
    Commented Jan 26, 2010 at 15:06
  • 1
    @mmyers Perhaps you have them mysteriously turned off? Or are using a teeny font size? There is definitely a vertical scrollbar for the first bit of code (starting with "class stringlist") when I look at it.
    – nb69307
    Commented Jan 26, 2010 at 16:08
  • @Neil: I'm guessing that Firefox/Chrome/whatever-you're-using auto-inserts scrollbars into <pre> areas then, because I'm certainly not seeing any in IE7. And the code area requires me to scroll quite a ways in the browser window, so it couldn't be font size.
    – mmyers
    Commented Jan 26, 2010 at 18:29
  • I can confirm that the scroll bars exist in Firefox 3.5.x, and Google Chrome 3.0.195.something, and do not exist in IE7.
    – Pollyanna
    Commented Jan 26, 2010 at 19:08
  • Wow, pre scrollbars are actually relatively complex: perishablepress.com/press/2009/11/09/perfect-pre-tags
    – Pollyanna
    Commented Jan 26, 2010 at 19:14
  • Hmm, interesting question here: stackoverflow.com/questions/439553/….
    – mmyers
    Commented Jan 26, 2010 at 19:19
  • Ah, here's the Jeff Atwood post about it: stackoverflow.com/questions/7707/…
    – Pollyanna
    Commented Jan 26, 2010 at 19:26
  • That looks IE8-specific, though. And that "none\9" hack is in the current CSS.
    – mmyers
    Commented Jan 26, 2010 at 21:36
  • Yep. Sounds like ie6 and ie7 are just thrown by the wayside like so much garbage, and the only hack is for ie8. On corporate desktops, one is often stuck with ie7 or (heaven forbid!) ie6.
    – Pollyanna
    Commented Jan 26, 2010 at 21:56