RichMarkdown › Code blocks in Markdown

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.

In short

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 toDo this
Inline codeType `code`, or select the text and use the code button in the toolbar
A code blockType three backticks and press Enter; add the language first, as in ```js
Leave the blockPress Enter twice at the end, or the Down arrow on the last line
Paste from Google DocsText 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

DestinationWhat arrives
Export .mdFenced blocks with their language
Download Word (.docx)A monospace font on a grey background
Save as PDF / printA code block; long lines wrap so nothing is cut off
A shared linkA code block; long lines scroll sideways
GitHub, ObsidianColoured by language

A block marked mermaid is a special case: it becomes a diagram. See Mermaid diagrams.

Common mistakes

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.

Open the free editor