Markdown Cheatsheet
Code
Use this Markdown reference while you build software engineering projects, review code, or refresh the syntax you reach for most.
Inline Code
Wrap the text in single backticks. Nothing inside is interpreted, so asterisks, underscores, and brackets stay literal.
Run `npm install` first. The `**kwargs` form stays literal inside a code span. Set `HOME=/tmp` before running.
Two details:
- Leading and trailing spaces are stripped, and internal runs of whitespace collapse to single spaces.
- One leading and one trailing space are stripped only if both are present, which is how you show a code span that itself starts with a backtick.
Backticks Inside Code
To include a backtick in a code span, use a longer run of backticks as the delimiter. Any number works, as long as the opener and closer match and the content does not contain that exact run.
Use `` ` `` for a single backtick. The `` ``code`` `` form uses two as the delimiter. Show a fence marker with ``` ``` ``` around it.
| Content to display | You type |
|---|---|
| One backtick | Double-backtick delimiters, with spaces around the content |
| Two backticks | Triple-backtick delimiters, with spaces |
| A run of n backticks | n+1 backtick delimiters |
The spaces beside the content are what stop the parser from merging your delimiters with the content. Many renderers, including this site's, only accept single-backtick code spans, so for anything with a backtick in it a fenced block is the safer choice.
Fenced Code Blocks
Three or more backticks on their own line open a block, and a matching or longer run closes it. Tildes work too in CommonMark.
Three backticks open, three close.
Tilde fences with ~~~ behave identically in CommonMark.
A fence opened with four backticks must be closed with four or more.The closing fence must be at least as long as the opener and must contain nothing else. A fence at the end of the document is closed implicitly, which is why a missing closer swallows the rest of a file.
Fences can be indented up to three spaces, and that indentation is stripped from every line of the content. Four spaces makes the opener itself part of an indented code block instead.
Info Strings
The word after the opening fence is the info string. Renderers read the first word as the language for syntax highlighting and ignore the rest.
| Info string | Effect |
|---|---|
bash, sh, zsh | Shell highlighting |
js, ts, jsx, tsx | JavaScript family |
py, python | Python |
json, yaml, toml, ini | Config formats |
sql, go, rust, java, c, cpp | Common languages |
diff, patch | Colored additions and deletions |
text, plain, txt, or nothing | No highlighting |
mermaid | GFM renders a diagram instead of code |
math | GFM renders LaTeX instead of code |
Tag the fence with a language so it highlights.
An untagged fence renders as plain monospace text.
Use text for output, logs, and directory trees.GFM also honors diff for review snippets, where lines starting with + and - are colored. Some renderers accept extra info-string tokens such as a filename or {1,3-5} line highlights, but that is renderer-specific, not GFM.
Nesting a Fence Inside a Fence
To show fence syntax itself, the outer fence needs more backticks than the inner one. Four outer, three inner is the standard pairing.
````markdown ```bash npm install ``` ````
The example above is indented two spaces so that this page's own renderer displays it rather than treating it as a real fence. In a normal Markdown file, write it flush left.
The same rule scales: to show a four-backtick fence, wrap it in five. It is also why documentation that embeds Markdown examples tends to standardize on four-backtick outer fences everywhere.
Indented Code Blocks
The original Markdown form: indent every line by four spaces (or one tab). It still works, and it has no info string, so no syntax highlighting.
def hello(): print("hi")
Reasons to avoid it:
- No language tag, so no highlighting.
- It cannot appear as the first block of a list item's content without colliding with the list's own indentation.
- A four-space-indented line anywhere becomes code by accident, which is the usual cause of a mysteriously monospaced paragraph.
- A blank line does not end it, so two indented blocks separated by a blank line merge into one.
Fenced blocks are better in every respect. The only place indented code still matters is inside a list item where you want a code block without a fence.
Code in Lists and Quotes
A fence inside a list item must be indented to the item's content column, which is 2 spaces after - and 3 after 1. .
- Install it: ```bash npm install ``` - Then run it: ```bash npm start ```
Under a blockquote, every line of the fence needs the > prefix, including the fence markers themselves.
Prefix every line of a quoted fence with a greater-than sign,
including the opening and closing fence lines. Dropping the
prefix on the closer leaves the fence open.Escaping and Gotchas
Inside a code span or a fenced block, nothing is Markdown. Backslash escapes are not processed either, so a backslash stays a backslash.
`\n is a literal backslash-n inside a code span`Common failures:
- Smart quotes. Some renderers apply typographic substitution to prose but not to code. If a copied command fails, check whether the quotes were curled outside a code span.
- Trailing whitespace in a fence is preserved, which can break a copy-pasted command in a shell that is sensitive to it.
- Tabs inside fences are preserved literally, which matters for
Makefileexamples where a tab is required. - A missing closing fence consumes the rest of the document. If everything after a certain point renders as code, look for an unclosed fence above it.
- Long lines in a fence do not wrap in most renderers, they scroll horizontally. Keep example lines under about 80 characters.