Code Blocks and Inline Code in Markdown
Wrap code in backticks for inline code, or in three backticks on their own lines for a code block. This guide covers syntax highlighting, language names, showing backticks inside code, and diffs.
Quick answer
How do you write a code block in Markdown?
Put three backticks on the line before the code and three on the line after. Add a language name after the opening backticks, such as ```python, to get syntax highlighting. For code inside a sentence, wrap it in single backticks: `code`.
```python
def greet(name):
return f"Hello, {name}!"
```
Call `greet()` with a name.Result
def greet(name):
return f"Hello, {name}!"
Call greet() with a name.
Code syntax and examples
Everything inside backticks is shown exactly as typed, so Markdown symbols in code are safe.
Inline code
Single backticks mark code inside a sentence: commands, file names and function names.
Run `npm install`, then open `src/index.ts`.Result
Run npm install, then open src/index.ts.
Fenced code block with a language
The language name after the opening backticks turns on syntax highlighting.
```js
const total = items.reduce((sum, item) => sum + item.price, 0);
console.log(total);
```Result
const total = items.reduce((sum, item) => sum + item.price, 0);
console.log(total);
Showing changes with diff
In a diff block, lines that start with - and + are colored as removed and added.
```diff
- const rate = 0.2;
+ const rate = TAX_RATE;
```Result
- const rate = 0.2;
+ const rate = TAX_RATE;
Backticks inside code
Use two backticks around inline code that contains a backtick, and a longer fence around a block that contains a fence.
``Use `code` for inline code``
````markdown
```js
console.log("hi");
```
````Result
Use `code` for inline code
```js
console.log("hi");
```
Tilde fences
Three tildes work like three backticks, which helps when the code itself is full of backticks.
~~~bash
echo "Wrap code in ``` to make a block"
~~~Result
echo "Wrap code in ``` to make a block"
Indented code blocks
Four spaces at the start of every line also make a code block, but without a language. Fences are clearer.
npm run build
npm run startResult
npm run build
npm run start
Code blocks in popular apps
Inline code and fenced blocks work almost everywhere; highlighting varies.
| App | Support | Notes |
|---|---|---|
| GitHub / GitLab | Supported | Highlighting for hundreds of languages; mermaid blocks become diagrams. |
| Discord | Supported | Fenced blocks are highlighted for the language you name. |
| Slack | Differs | Code blocks work, without highlighting. |
| Obsidian | Supported | Highlighting in reading view; mermaid blocks become diagrams. |
| Notion | Differs | The /code command adds a code block; the language is picked from its label. |
| Differs | Fenced blocks work on the new site; old Reddit needs four-space indentation. |
Questions and answers
How do I write inline code in Markdown?
`npm install`. To show a backtick inside, wrap the code in two backticks with spaces: `` a`b ``.How do I add syntax highlighting?
```python or ```js. The app must support highlighting and know the name.Which language names can I use?
bash, c, cpp, csharp, css, diff, go, html, java, js, json, kotlin, markdown, php, python, ruby, rust, sql, swift, ts and yaml. GitHub accepts hundreds more.How do I show triple backticks inside a code block?
~~~.Why is my code block not rendering?
Can I show line numbers?
How do I make a diagram from a code block?
mermaid and write the diagram in Mermaid's syntax. GitHub, GitLab and Obsidian draw it, and so does Markdown Live Preview.