Forum Code Formatting and Syntax Highlighting

To format code in this forum you need to surround the code with three backticks (```). For example, typing this...

```
fn main() {
    println!()
}
```

...will be rendered as highlighted Rust code, like so:

fn main() {
    println!()
}

You can also tell the forum software which language you're using by adding the language after the opening backticks.

```cpp
#include <iostream>
int main() {
  std::cout << "Hello World!";
}
```

Will appear with highlighting as:

#include <iostream>
int main() {
  std::cout << "Hello World!";
}

The currently supported languages include: apache, bash, cs, cpp, css, coffeescript, diff, xml, http, ini, json, java, javascript, makefile, markdown, nginx, objectivec, ruby, perl, php, python, sql, handlebars, rust and dust. Others may be added in the future.

It's also possible to add plain text by using text as the language:

```text
Error 365: Invalid command.
See help for a list of valid commands.
```

This will be rendered without any highlighting:

Error 365: Invalid command.
See help for a list of valid commands.

If your keyboard layout does not have the ` key, you can also use a ~.

~~~
fn main() {
    println!()
}
~~~

...will be rendered as highlighted Rust code, like so:

fn main() {
    println!()
}
68 Likes

Seems like pinning this thread would be fine?

11 Likes

This is part of "markdown", a standard text formatting syntax not unique to Github. These days most implementations I know of are using the CommonMark spec with some extentions. Here are some links:

I've also personally used ```s and other bits of markdown on StackOverflow, Slack and Discord, so it's pretty close to universal.

15 Likes

Since this has now been pinned (thanks!) I've removed the last note that lxrec is responding to, but I'll copy it here for posterity:

Summary

However this way of posting code isn't so obvious. It relies on a new user being aware of the github syntax and guessing that this forum uses the same syntax. I didn't when I first arrived and frequently new users post code that's hard to read because they aren't aware of how to format it for this forum's software.

Could there be somewhere that explains this placed where new users can see it?

I didn't realize that the syntax was now in the CommonMark spec. Thanks for the correction.

4 Likes