# Syntax

Code in Markdown: inline and fenced blocks

Last updated

To show code in Markdown, wrap a short snippet in single backticks for inline code, or put it between two lines of three backticks for a fenced code block. Inline code sits in the middle of a sentence. A fenced block stands on its own and keeps every line, space, and symbol exactly as you typed it.

Both do one job: tell Markdown "treat this as code, not prose." Otherwise characters like *, #, or _ get read as formatting. Backticks switch that off and show the raw text.

Inline code

Use inline code for a single command, a filename, a function name, or any short bit of code that lives inside a normal sentence.

Wrap it in one backtick on each side:

Run `npm install` first, then open `index.html` in your browser.

Now npm install and index.html appear in a monospaced font, set off from the words around them. Nothing inside the backticks gets formatted. An underscore stays an underscore instead of turning into italics.

Need a literal backtick inside your inline code? Wrap the whole thing in two backticks instead of one:

Use `` ` `` to start a code span.

The outer double backticks let the single backtick in the middle show as plain text.

Fenced code blocks

Use a fenced code block for anything longer than a word or two, or for code you want to keep on its own lines.

Open with three backticks on their own line, write your code, then close with three backticks on their own line:

```
function greet(name) {
  return "Hello, " + name;
}
```

Everything between the fences is preserved as written. Line breaks and indentation stay put, and no character inside gets treated as Markdown. The block renders as its own box, separate from the paragraphs around it.

Adding a language tag

Write the language's name right after the opening three backticks. That word is the language tag. It tells whatever displays the file which kind of code the block holds, so the code can be colored to match.

```python
def greet(name):
    return f"Hello, {name}"
```

The word python never shows up in the rendered block. GitHub, code editors and Markdown apps that color code read it and apply Python's rules, so def and the quoted string each come out in their own color. Common tags include js, python, swift, bash, json, html and css.

The tag is optional. A fence with no tag still renders as a code box, usually without color, and so does one whose tag the renderer doesn't recognize.

Tildes instead of backticks

If your code itself contains a line of three backticks, fencing with backticks would close the block early. The fix is to fence with three tildes (~~~) instead:

~~~
Here is a markdown example:
```
some code
```
~~~

Three tildes open and close a fenced block exactly like three backticks. Mix the two and you can show backtick fences inside a code block without breaking it.

In Unmarked

Inline code and fenced blocks both render, with backtick or tilde fences. A fenced block is colored by its tag in your theme's code colors, in Read mode and as you type in Write, and a block with no tag stays plain. In Write mode, a copy button appears on a block while your cursor is inside it.

What Markdown Unmarked renders explains which languages it colors and how code looks in exports and how code looks in exports.