Wiki - how to post code on this forum

This thread is intended as a short help file for all those struggling to post formatted code on the forum.

Code block formatting

Posting formatted code on the forum (and most others) is as simple as selecting the code and pressing the </> button. It works the same as applying Bold or Italic styles to your document. It preserves indentation and applies syntax highlighting.

How to post code

Note that all the </> button does is add three backticks ``` on the line before the code block and another ``` on the line after. If you wish you can type these rather than use the button.


Inline code formatting

Enclose the code snippet in single backticks, `...`.
Inline code formatting

15 Likes

Collapsing code / hide details

Code can also be collapsed using the Hide Details feature. This can tidy up a thread with multiple code blocks and provides a ready-made index within the post.

Hide details

8 Likes

This almost needs to be inbuilt into the reply editor, and suggested whenever the editor sees unformatted code.
Good job on the topic though; something to bookmark for regular use :stuck_out_tongue_winking_eye:

7 Likes

Just installed this...we'll see if it helps.

11 Likes

This excited me way too much, haha

Can you fix my code?

def foo():
doTheStuff += 1

Noooooo

1 Like

What about a copy/paste of code instead of writing it in here like a n00b...

def generateDataCSV():
data = project.reports.tankStatus.getData()

fileName = "Tank Status Report {}".format(system.date.format(system.date.now(), "yyyy-MM-dd HHmm"))

Edit: hmm, not sure it's working, I don't get any prompts :frowning:

2 Likes

It doesn't show up for "trust level" 3 or higher, by default.

1 Like

Makes sense!

Looks like it isn't working,

3 Likes

Can you conditionally pin threads like this for newer members? Probably would be helpful if this was always at the top for people until they've made a few posts.

3 Likes