40

Recently I've seen a lot of code being formatted as quote. This is not how it should be done.

All code should be formatted with one of two styles.

  1. If it is just 1 line, with other text around it, use the ` ` formatting, like this:

    Hello. This is code and continued sentence.

  2. If it is one line, but on its own, it should have 4 spaces before it:

    Hello. This is code
    

    To save adding the 4 spaces to each line manually, highlight your block of code and then click the { } button on the editing toolbar or press CTRL+k.

  3. If it is multi line, use the 4 spaces before each line:

    Hello.
    This is code
    

    To save adding the 4 spaces to each line manually, highlight your block of code and click the { } button on the editing toolbar.

  4. If it's multi line and in a list, indent the code 8 spaces:

    • Hello
    • This is a list

      Hello
      This is code.
      

How not to do it.

  1. Accent (``) formatting on a multi-line code block:

    Hello. This is badly formatted code

  2. Quote format (even if you append the 2 spaces):

    Hello. This is badly formatted code

  3. 2 line breaks between each line:

    Hello.
    
    This is badly formatted code
    
  4. No formatting. Seriously, this is so hard to read. Compare this with this:

    Hello. This is badly formatted code

  5. HTML tags (<code>)

    Hello. This is badly formatted code

    HTML tags (<pre>). It looks okay, but is less nice to edit.

    Hello.
    This is badly formatted code
    
  6. Using speech marks

    "Hello. This is badly formatted code"

Reminder:

All code should be between ` characters (grave accent, often found below the ESC key), or have 4 spaces at the start of the question.

`Only format inline code with this syntax`
    Format multiline code with this syntax
18
  • 14
    Meta notice is nice but how many users do you think will comply :P
    – Thomas Ward Mod
    Commented Jun 11, 2015 at 12:17
  • 1
    @ThomasW. few :( I'm linking it in the comments whenever I see it.
    – Tim
    Commented Jun 11, 2015 at 12:38
  • 3
    I'm not sure about your first rule there. If your code is on its own (eg not inline) it should use the 4-space indentation it's to be formatted as a block. Only use inline code for inline code.
    – Oli Mod
    Commented Jun 11, 2015 at 13:41
  • 12
    Why not directly link the markdown instructions: askubuntu.com/editing-help
    – s3lph
    Commented Jun 11, 2015 at 14:04
  • @the_Seppi I thought it was a little confusing, and this is specifically to say not to use quote formatting.
    – Tim
    Commented Jun 11, 2015 at 14:25
  • I am missing: "1 long line more than fits on screen
    – Rinzwind
    Commented Jun 11, 2015 at 14:44
  • See also meta.askubuntu.com/questions/13592/…
    – Carl H
    Commented Jun 12, 2015 at 7:24
  • 2
    You know you can select a block of code / text and use the formatting buttons at the top ?
    – Panther
    Commented Jun 12, 2015 at 14:10
  • 3
    On the editor, highlight the entire code excerpt you want to format and click the button marked {}. If it's a multi-line excerpt this will automatically add the four spaces at the start of every line for you. MUCH more convenient than adding the spaces yourself, if your code excerpt is more than a couple of lines. Commented Jun 16, 2015 at 2:31
  • 1
    About the grave accents, use the four spaces in front of the line if grave accents are part of your code. I really do like what you wrote here, and I learned something new today! :)
    – Terrance
    Commented Jun 16, 2015 at 13:31
  • Thanks for the suggestions. There are a few bugs with your suggestions and the parsing of markdown, however. For example, try having a four-space-indented block following a list, or vica-versa.
    – earthmeLon
    Commented Jun 16, 2015 at 19:07
  • @ThomasW. (and Tim) probably users that read Meta format it properly anyway...
    – Wilf
    Commented Jun 18, 2015 at 19:12
  • @Wilf indeed, that's probably the case.
    – Thomas Ward Mod
    Commented Jun 18, 2015 at 19:41
  • When I used askUbuntu for the first few posts I was missing that kind of info. Maybe there is a way to put a link to that into the profile page of new users?
    – CatMan
    Commented May 13, 2018 at 22:13
  • @CatMan This information is visible in the sidebar when you ask a question. It is not as clear when you post an answer. Wherever we put the link, 99% of new users won't read it, unfortunately.
    – Tim
    Commented May 13, 2018 at 22:15

2 Answers 2

16

There is one special case where <pre> may be desirable instead of 4 spaces:

This is the important part of the program

Notice that I can use <strong></strong> within the block of code when I use <pre>. However, if I try to do that with the 4-space indent, all I get is:

This is the <strong>important</strong> part of the program
2
7

If you put a code block after a list, the code block isn't formatted properly:

  • bullet

    code block

You can resolve this by indenting the code block 8 spaces, which nests it under the bullet:

  • bullet

    code block
    

Or put something invisible between them, like an HTML comment <!-- comment --> or a Markdown comment <% comment %>, which doesn't nest it:

  • bullet
code block
0

You must log in to answer this question.

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