Markdown cheatsheet · Code
Code in markdown: inline and blocks
Single backticks for a snippet inside a sentence; a fence of three backticks for a block. Nothing between the fences is interpreted as markdown.
Inline code
Press `Cmd+P` to open any note.
Press Cmd+P to open any note.
Fenced code blocks
Three backticks on the line before and after. Add a language after the opening fence and renderers that support highlighting will color the code; the fence works fine without it.
```python
def greet(name):
return f"Hello, {name}"
```def greet(name):
return f"Hello, {name}"
Common language tags: js, ts, python, rust, go, bash, json, html, css, sql, diff. Three tildes ~~~ work as a fence too. One tag is special: mermaid isn't highlighted, it's rendered as a diagram where supported.
Backticks inside code
To show a backtick inside inline code, wrap the code in double backticks: `` a `backtick` inside ``. To show a three-backtick fence inside a block, make the outer fence longer (four backticks), which is also how you quote markdown source without rendering it:
```` ```js // this fence is displayed, not parsed ``` ````
```js
// this fence is displayed, not parsed
```
The indented form (and why fences won)
Original markdown also treats any line indented by four spaces as code. It still works, but it's fragile: no language tag, and it fights with list indentation. Prefer fences; mostly you need to know this rule so a random four-space indent doesn't turn your prose into code by accident.
Common questions
Why is markdown still rendering inside my "code"?
The fence probably isn't alone on its line, or the opening and closing fences have different indentation. A fence must be at the start of its line (up to three spaces allowed), with the closing fence at least as long as the opening one.
Can I highlight specific lines?
Not in standard markdown or GFM. Some site generators add their own syntax for it (```js {2,5}); check your tool's docs, and expect it not to travel.