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.

4
  • A simple way: type the code, select the code, press Ctrl + K. And you should ask this question on Meta.
    – Yu Hao
    Aug 15, 2013 at 5:34
  • 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:

The $ character is just a shortcut for window.jQuery. 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.
    

Stack Overflow guide.

2
  • 7
    Where did jQuery come from?
    – Paul
    Aug 15, 2013 at 5:36
  • 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
1

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

3
  • 1
    Unfortunately, this does not work for me.
    – user24601
    Aug 11, 2018 at 17:02
  • @user24601: Yes, it (sometimes) fails (due to failed loading of some JavaScript code?). Jul 31, 2020 at 9:53
  • I finally figure out the magic. First paste your code, then highlight it all, then finally press Ctrl-k.
    – Mike
    Jun 22 at 18:41