Add a code block to a document
Put code in a document with its spacing kept in a fixed-width font, choose the language that colours it, and copy the code.
Choose Format → Code block, or type ``` and Enter. Typing
```js and Enter starts a code block in JavaScript.
Code keeps its spacing and shows in a fixed-width font. Enter starts a new line in the block. Enter on an empty last line leaves the block for a new paragraph.
Choose the language and copy the code
A code block's corner has two buttons:
- The first names the block's language. Select it to colour the code for Bash, C, C#, C++, CSS, Go, HTML, Java, JavaScript, JSON, Kotlin, PHP, Python, Ruby, Rust, SQL, Swift, TypeScript, XML or YAML.
- Copy code copies the block's text.
Both are also on the block's right-click menu, as Copy code and Code language…. Format → Code language… chooses the language of the block with the cursor.
A new code block starts in the language of the code block before it. Ctrl/⌘ Z straight after choosing a language puts the last one back.
Colours in the PDF and in exports
The PDF, the shared reading page and Word export show the colours. Markdown and HTML export also keep the language's name. Word has nowhere to keep the language, so a code block comes back from Word without one. In an older browser the code shows in one colour.
If a code block does not show its colours, Contact support.
Ask Magpie support
Send Magpie staff a question about anything this article does not answer.