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

What is the best way to copy large amounts of code into Stack Overflow?

For up to 10 lines, prepending 4 spaces to each line is ok.

For 50 or 100 lines it become a headache.

For more, this is probably not the correct way to do it.

What do you think?

share|improve this question

Select the snippet and hit Ctrl+K or click the code button on the tool bar. Or wrap the section with <pre><code> tags.

Also, I agree with the sentiment that if you have a lot of code, you're likely doing something wrong. However, I disagree on what makes for a lot of code. 20 lines might not be very much. If we're going to use lines for the metric, then 40 to 60 could still be reasonable.

share|improve this answer
Out of interest, can you find a question with 40-60 lines of code which could not have been simplified to narrow down the focus to the actual problem being explored? Maybe I've just seen too many "here's all my code, and there's a problem on line 1375" to remember the legitimate ones. – Dominic Rodger Oct 12 '09 at 20:53
@Dominic: Are you assuming the code is in a question rather than an answer? I've certainly posted over 40 lines within an answer before, when I want to post a complete implementation to help the questioner - or a benchmark. – Jon Skeet Oct 12 '09 at 21:26
@Jon - Yes. Just reread the title and feeling a bit silly :-) – Dominic Rodger Oct 13 '09 at 5:18

Joel is right - you can hit Ctrl-K to indent a block of code so long as it's not indented already. If it's already indented, there's no way of indenting it another 4 spaces - it will be outdented instead.

Personally I'd like to see separate indent and outdent shortcut keys (probably keeping Ctrl-K with its current behaviour to avoid confusing people too much). As it is, I suspect either a greasemonkey script or manipulating the code elsewhere is the best way forward :(

share|improve this answer
You can un-indent just the first line, highlight the whole block, hit ctrlK, and it will push everything over. Then fix the first line again. It's better than doing the whole thing line by line or pasting from an intermediary app first, anyway. – Joel Coehoorn Oct 12 '09 at 20:23
@Joel: Yes, I've done that before - but it's a pain if you're trying to add one block that needs to be indented 4 times or something like that. Better shortcut support would be much better IMO. – Jon Skeet Oct 12 '09 at 20:32

Don't post hundreds of lines of code. Don't even post fifty. Abstract you problem down to a quantity that can be understood in short order: that is find the minium piece of source that will reproduce the problem.

Yes, this is some work, but

  1. You might find the answer while you are doing it
  2. It proves that your not asking us to read a pile of code because you are lazy.

And if you are, somehow, posting a long code for a good reason, select it a hit to 101010 button (or use control-k (which always screws me up with my emacs reflexes)).

share|improve this answer
Good point about trying to distil the problem down to the minimum reproducible code. I've found that this has worked for me in the past. – ChrisF Oct 12 '09 at 21:52

I you would like to supplement your shortened example code, you can use a free service like PasteBin to put your code and link to it in your answer.

share|improve this answer

I hope you're not actually writing 50-100 lines of code in the Markdown editor. Especially if you're using a language where indentation matters.

When formatting code snippets for SO, I use an editor with indentation settings set at 2 spaces. This keeps lines short. And also makes it easy to indent everything by four spaces when I'm done...

share|improve this answer

The best way in my opinion is the "It's All Text!" plugin for firefox. That way I can use gvim and get all the >> and << goodness I'm used to.

The main drawback is the loss of the WYSIWYG stuff.

share|improve this answer

If you've got more than about 10 lines of code, your question is almost certainly too broad, certainly if you've got more than 20 lines of code.

share|improve this answer
An answer may reasonably contain that much code though - for example, a full collection class implementation. – Jon Skeet Oct 12 '09 at 20:17
I agree with Jon. Some do require a bit more code, but still can be to the point with the question. – Troggy Oct 12 '09 at 20:20
@Jon Skeet - agreed. I know that I have a hard time not hitting back when I see a question with more than around 20 lines of code. It takes an act of the will (or an above usual amount of reluctance to do whatever it is I should really be doing ;-)) to read the question. Maybe that's just down to my tendency to prefer easy to digest questions - most of my visits to the site probably last around 5 minutes each. – Dominic Rodger Oct 12 '09 at 20:51

You must log in to answer this question.

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