13

How to format code in posts on Stack Overflow? I am getting frequent error messages while submitting posts with code. I am getting the following message:

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 ? tool bar icon.

I have tried this by using ctrl+k -->enter code here and entered the code but I am getting the same error message. Also, I have tried using ">" before every line, even then the same message appears.

3
  • Even here, this information is very searchable. For example, there are guidelines when writing your post with a link to more information.
    – chris
    Aug 15, 2013 at 5:35
  • There's a ? icon at the top right of the box where you entered your question. Click it, then click "advanced help". Aug 15, 2013 at 6:12
  • stackoverflow.blog/2014/09/16/…
    – gman
    Mar 1, 2020 at 16:12

2 Answers 2

4

To create code blocks or other preformatted text, indent by four spaces:

This will be displayed in a monospaced font. The first four spaces
will be stripped off, but all other whitespace will be preserved.

Markdown and HTML are turned off in code blocks:
<i>This is not italic</i>, and [this is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

If you want to have a preformatted block within a list, indent by eight spaces:

  1. This is normal text.

  2. So is this, but now follows a code block:

     Skip a line and indent eight spaces.
     That's four spaces for the list
     and four to trigger the code block.
    
2
  • This is the most frustrating part of using StackOverflow for me. For YEARS, I still cannot get this to work. GitHub is so easy. Why not use support GitHub markup.
    – user24601
    Aug 11, 2018 at 17:02
  • CommonMark code blocks are now supported. I'm not sure since when.
    – starball
    Jan 8, 2023 at 8:21
2

Highlight or select the code, and press Ctrl + K.

0