I spend a bit of time some days editing and improving the quality of questions and answers, and confortably the most common task is to format a block of code where the questioner/answerer has failed to do so.

Moreover, it's clear that many new users just don't understand how to do so. Check out the comments on the following question


The problem of getting people to format code correctly in their posts seems to be endemic. For me there are a number of issues:

  1. The posted code is incomprehensible.
  2. In the case of HTML tags the posted code can actually be hidden.
  3. Tabs are pasted as tabs, and spaces as spaces making formatting even more difficult.

I have a few thoughts regarding this:

  1. A warning message is displayed when you make a post that looks like it contains unformatted code (akin to the warning when the system deems you should start voting for answers).
  2. Convert all tabs to (4?) spaces when posting (client-side clipboard access issues make it difficult to do so on pasting).
  3. Add a new tab the the 'review' page to highlight potentially unformatted code.
  4. Add something to the FAQ with regard to code formatting.

Does anyone have any more, or thoughts of their own regarding this?

  • 1
    Also, use the new force-feedback feature for some negative reinforcement (Zzap!) when someone writes all their variable names in bold.
    – jscs
    Commented May 25, 2011 at 15:04
  • Hard to say what the problem is now. There's plenty of information made available on how to format code. I understand some of it they have to click through before posting a question. That, of course, doesn't guarantee that they'll read it, but neither do any of your solutions. The incredulity here is misdirected. Just vote those questions down. If users don't try to help themselves, why should we try to help them? Commented May 26, 2011 at 8:51
  • @Cody - Didn't realise that there were things that they had to click through before posting, however there is still nothing in the FAQ. Given that 'how do I format code' seems to be relatively 'frequent' then maybe it should go there. As for not helping those that can't help themselves - isn't one motivation helping others and making the internet a better place to get expert answers? I thought that was the whole reason behind the editor badges Commented May 26, 2011 at 9:29
  • 1
    @james only for users who are willing to put in some effort of their own. Our goal is not to encourage laziness. Commented May 27, 2011 at 18:50
  • @Jeff Atwood - Its been a wee while, but I've not forgotten about this :-) A lot of the edits I do are still regarding code formatting. If we're not to encourage laziness, then should editors stop correcting the code-formatting in questions? Put another way, doesn't the fact that helpful SO users are editing these questions mean that we are already actually encouraging laziness? Commented Jun 13, 2011 at 12:16
  • @james it depends if the rest of the post is good and clear and worth saving; in my experience there is a substantial amount of "turd polishing" that goes on, however. Don't hesitate to use your downvotes (free on questions) and close votes as well. Commented Jun 13, 2011 at 21:02
  • @Jeff: "I got really tired of performing this edit over and over" meta.stackexchange.com/questions/2950/…. This is how I am starting to feel with the code-formatting. I don't know how others feel, but I'm not really seeing any improvement. Have we really exhausted all the feasibile possibilities? Commented Jun 27, 2011 at 12:43
  • fine, give it a shot yourself: programmers.stackexchange.com/questions/87611/… Commented Jun 28, 2011 at 8:21

3 Answers 3


While not all users are fixable, it's correct that the new and improved Markdown help doesn't make it clear that the buttons can be used to simplify much of the formatting work.

We're changing this – actually, we're bringing back an old friend to help us with this.

It's already deployed here on Meta; try it out: Open the [?] Markdown help and click on the tabs (you might have to refresh the page before).

enter image description here

  • oh no you didn't, @Jeff
    – balpha StaffMod
    Commented May 27, 2011 at 10:33
  • That's a step in the right direction, but I barely noticed the bouncy bit. Why can't you just add some text to the help?
    – McKay.CPP
    Commented May 27, 2011 at 11:20


Simple method for reliably detecting code in text?

We just deployed a method that we think is quite reliable for blocking 98% of posts submitted with code that has improper code formatting (or no code formatting at all).

For example, when I go into Chrome incognito and attempt to ask this question body on Stack Overflow right now:

I needed to add up all the rows for a result. Using the select_sum as follows

Here is the model

function Dues_Paid_Tot($date)
        $query = $this->db->select_sum('Dues_Paid', 'Dues_Paid_Tot');
        $query = $this->db->get('Membership');
        return $query->result();

Here is the controller

function Fiscal2()
    $date = $this->input->post('Select_Date');
        if($query = $this->report_model->fiscal_list($date))
            $data['records'] = $query;
    $data['date'] = $this->input->post('Select_Date');
    $data['Dues_Paid_Tot'] = $this->report_model->Dues_Paid_Tot($date);
    $data['main_content'] = 'report_fiscal_view';
    $this->load->view('includes/template', $data);

I get back:

Oops! Your question couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

This check is limited to users with <= 50 reputation and is performed on questions / answers / edits and suggested edits.

If you see or hear of any cases where this is triggered inappropriately -- or not triggered, and it should have been -- let us know.

  • Brilliant Jeff! Are you intending to post the solution as answer in the linked question, or accept the one you used? Commented Jul 13, 2011 at 12:59
  • @james the accepted answer is very close to what we do, though we look at individual characters, position within the line, and string sequences together. Line length is also important. Commented Jul 15, 2011 at 6:31

IMO the markup needs to be extended to allow formatting a block of code without having to prefix every line with 4 spaces. I've just started using StackOverflow -- and I'm already finding it tedious to add that to every code line.

To post working code into my question, in my emacs buffer I have to M-x replace-regexp "^J" with "^J " before pasting the text the edit box for it to format correctly. I tried the <pre></pre> block tags, but that did not work for me.

If there were <code></code> block tags that I could wrap code with it would be much simpler to cut and paste code real code that I am working on in a editor.

Also, the preview of the posting does not syntax highlight the code, if it did I would know immediately if it were parsing my markdown correctly.

edit: If this suggestion were implemented, then I wouldn't have had the experience described above. edit 2 here: I somehow pasted the wrong link and meant to link to this suggestion

I still think that the preview box should syntax highlight.

Also, wouldn't it be possible to detect code snippets that weren't formatted and automatically 'suggest' that they be formatted so? I'm sure there is an ugly regexp out there that detects code written in most languages....

edit 2 Negative votes? Hey, I'm just a new user to the site sharing my very fresh on topic experience about code formatting. Other users have run into the same issue. sheesh

I realize that my suggestion about the <code> block tag was naive -- but then again maybe not. Can someone point me to a help entry discussing the <code> tag? I really cannot find one.

As a new user, I wanted to paste a bit of code into my question and I only had read the quick help box accessible by the [?] button. I was under the impression that the only option I had was to manually insert 4 spaces before every line to get code to format correctly. If I were a novice programmer copy-pasting more than 15 lines of code I would have given up and pasted it without any formatting.

Its been pointed out before, and I'll add my 2 cents:

Both markdown Help sections (the quick help and the advanced help>> page) NEED to have better documentation of:

  • highlighting existing text and pressing the {} button or Ctrl-K
  • the <code></code> tags. The quick help makes no mention of it and the wording on the advanced help>> mentions it in passing when discussing the backticks markdown syntax.
  • There's a problem with Ctrl+K or the {} code sample button on the editor? Commented May 26, 2011 at 22:37
  • 2
    No problem :) I just wasn't aware of them. The 'Code' tab under the extended help by the input box doesn't point those out.
    – McKay.CPP
    Commented May 27, 2011 at 2:39
  • <code></code> works!
    – jscs
    Commented May 27, 2011 at 3:06
  • I guess since I use 4 spaces per tab already I never noticed.
    – user7116
    Commented May 27, 2011 at 3:34
  • I honestly don't see what different syntax highlighting would make: you can clearly see whether there is a pre or not in the preview.
    – Benjol
    Commented May 27, 2011 at 6:20
  • @Josh Yes, I realize that now. But as I said, the 'Code' section of the help above the text entry field does not mention <code></code>. I shouldn't have to search all over the site looking for help w/ markdown.
    – McKay.CPP
    Commented May 27, 2011 at 9:07
  • @McKay: agreed about lack of documentation. I just wanted you to know it was available.
    – jscs
    Commented May 27, 2011 at 9:09
  • @Benjol Its not clear to me if there is a <pre> section in the preview. I'm using Chrome on Linux and it blends right into the background. Also, the preview does not update instantly. I'm not sure if its a chrome bug or what, but I've seen it markdown code that I've definitely put into a <pre> or <code> block.
    – McKay.CPP
    Commented May 27, 2011 at 9:10
  • @McKay, that's weird. It also changes the font to mono-spaced, do you get that?
    – Benjol
    Commented May 27, 2011 at 9:13
  • @Benjol No, I don't always get mono-spaced font. I just created a new question discussing what I think is a bug in the <code> markdown.
    – McKay.CPP
    Commented May 27, 2011 at 10:13
  • @sixlettervariables I ran into the problem right away because my first question involved C preprocessor macros, which have to start in the first column. I also use two spaces per tab.
    – McKay.CPP
    Commented May 27, 2011 at 10:24
  • we changed some things based on this feedback. See balpha's answer. Commented May 27, 2011 at 10:37

You must log in to answer this question.

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