Code blocks in Markdown
Updated
Markdown has two kinds of code: short pieces inside a sentence, wrapped in single backticks, and whole blocks fenced by three backticks. Everything inside is shown exactly as typed, so symbols like * and # stay as they are.
- Inside a sentence:
`npm test`. - A block: three backticks on the line above and the line below. Add a language after the opening fence, such as
```js. - In the editor, type three backticks and press Enter. Press Enter twice to leave the block.
Inline code
Run `npm test` before you commit.
If the code itself contains a backtick, wrap it in two backticks with spaces just inside: `` a`b ``.
Fenced code blocks
```python
def greet(name):
return f"Hello, {name}"
```
The word after the opening fence names the language. Sites such as GitHub use it to colour the code. Common names are js, ts, python, bash, json, html, css, sql and yaml.
Showing a fence inside a code block
Use a longer fence on the outside. Four backticks can hold three:
````md
```js
let a = 1;
```
````
Other forms
Fences can also be written with ~~~, and older Markdown marks code by indenting each line four spaces. RichMarkdown reads both and writes them back as standard backtick fences.
Code in RichMarkdown
| You want to | Do this |
|---|---|
| Inline code | Type `code`, or select the text and use the code button in the toolbar |
| A code block | Type three backticks and press Enter; add the language first, as in ```js |
| Leave the block | Press Enter twice at the end, or the Down arrow on the last line |
| Paste from Google Docs | Text in a monospace font arrives as inline code |
RichMarkdown keeps the language in the file but does not colour code in the editor. The colours appear where the Markdown is published, for example on GitHub.
Where code goes next
| Destination | What arrives |
|---|---|
| Export .md | Fenced blocks with their language |
| Download Word (.docx) | A monospace font on a grey background |
| Save as PDF / print | A code block; long lines wrap so nothing is cut off |
| A shared link | A code block; long lines scroll sideways |
| GitHub, Obsidian | Coloured by language |
A block marked mermaid is a special case: it becomes a diagram. See Mermaid diagrams.
Common mistakes
- A fence that is never closed. Everything after it becomes code. Add the closing three backticks.
- Curly or full-width quotes in place of backticks. The backtick is usually the key to the left of 1.
- A space in the language name. Write
```pythonwith one word after the fence. - Formatting inside code.
**bold**in a code block stays as typed. That is the point of a code block.
Related guides
FAQ
How do I add a code block in Markdown?
Put three backticks on the line above the code and three on the line below. Add a language name after the first three to get colours on sites such as GitHub.
How do I write a backtick inside inline code?
Wrap the code in two backticks, with a space just inside each end.
Does RichMarkdown colour my code?
Not in the editor. It keeps the language name in the file, so GitHub, Obsidian and similar tools can colour the code.
How do I get out of a code block in the editor?
Press Enter twice at the end of the block, or press the Down arrow on its last line.
Does code stay readable in Word?
Yes. Download Word writes code in a monospace font on a grey background.