How to post code with more than 1 line?

Hi,

my question is: How can I post a code snippet with more than 1 line. I’m using the code-Tags.

sample:

write "line1" write "line2" end

Believe me: There is a newline between the two write-statements.

Regards

Matthias

Hi Matthias,

To me the easiest is to:

  1. On a new line click the code button image
  2. Paste the code in the place of type or paste code here i.e.
write "line1" 
write "line2" end
2 Likes

If you don’t want to use the button you can also Instead, use 3 backticks (```) on their own line above and below your code. Be sure there are no spaces on the lines with the backticks, or this will not work.

Example:

Typing:

```
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
```

Produces:

public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}

Automatic code styling

The forums will try to automatically detect what language you are typing and style your code accordingly. This might be nice for sharing code, but tends to produce strange results when sharing other preformatted text. To avoid the automatic styling, declare your code to be “raw text” by typing text after the first 3 back ticks.

Example:

Typing:

```text
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
```

Produces:

public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}

In addition to text , you can manually choose the formatting of a code block with the following selectors: apache , bash , coffeescript , cpp , cs , css , diff , handlebars , http , ini , java , javascript , json , makefile , markdown , nginx , objectivec , perl , php , python , ruby , sql , xml .

2 Likes

Finally you can use [code][/code] i.e.

[code]
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
[/code]

will produce:

public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
2 Likes

this [code]-thing didn’t work.
But now I know, why.

[code]line 1
line 2[/code]

results in

line 1 line 2

while

[code]
line 1
line 2
[/code]

results in

line 1
line 2

… strange.