How to Put a Backtick Inside Code in Markdown — Tested in Six Engines
Inside Markdown code, a backslash prints as a backslash, so \` cannot escape a backtick. Change the delimiter instead. For inline code, wrap it in a longer run of backticks than it contains, with one space inside each end. For a code block that contains a fence, open the outer fence with four backticks or with tildes.
The question has been asked on Stack Overflow at least six different ways since 2014. Those six threads hold 96,620 views between them as of 30 October 2026; the most-voted, "How does one escape backticks in markdown?", has 30,852 views and 104 votes 1. A long tail, not a headline — and a stubborn one.
The newer version of the problem arrives from an AI chat. You ask for a README, the answer contains a fenced block inside a fenced block, and the reply breaks in half at the inner fence.
The fix is older than either. John Gruber's 2004 syntax page already gave it — multiple backticks as the delimiter 2 — and John MacFarlane's CommonMark spec made it exact 3. This reference runs every case through six engines in ten renderer modes on 30 October 2026, and shows what each wrong answer actually does to the page 4.
The quick reference: what to type for each case
Four cases cover almost every backtick problem. One backtick, or a run of them, inside inline code: wrap it in a longer run plus a space. Code that starts or ends with a backtick: the space is mandatory. A fence inside a fence: make the outer fence longer, or switch to tildes. Never reach for a backslash.
| You want to show | Type this | Same result in |
|---|---|---|
| A word with backticks around it, as code | `` `a` `` | 10 of 10 modes |
| One backtick on its own, as code | `` ` `` | 10 of 10 modes |
| Three backticks, as inline code | ```` ``` ```` | 10 of 10 modes |
| Code that starts with a backtick | a space after the opener — never ```a`` | spaced form 10 of 10; unspaced 3 different outcomes |
| A fenced block inside a fenced block | outer fence of ```` (four backticks) | 9 of 10 modes |
| The same, with tildes | outer fence of ~~~ | 9 of 10 modes |
| A backslash before the backtick | — do not use | backslash printed, 10 of 10 |
The "9 of 10" rows are not a disagreement about the fix. The tenth mode is Python-Markdown with its default extensions, which has no fenced code blocks at all 5.
Every row is measured, not inferred. The inputs and outputs are listed under the method section below.
Why a backslash cannot escape a backtick inside code
Inside a code span or a code block, a backslash has no special meaning. The backslash you type is printed. The backtick after it is read as an ordinary delimiter — so the code span closes one character early, and the rest of the line rearranges itself. Nothing errors. The line just reads wrong.
The CommonMark spec says it in one sentence: "Note that backslash escapes do not work in code spans. All backslashes are treated literally" 3. GitHub's spec carries the identical sentence 6. The spec's own Example 338 shows the damage in miniature: `foo\`bar` renders as code foo\ followed by plain bar and a stray backtick.
Here is the same mistake in a full sentence, the way people actually type it:
Type `a\`b` here, then `c` later.
And here is what every one of the ten renderer modes produced 7 — GitHub's comment mode only adds a class attribute to each code tag:
<p>Type <code>a\</code>b<code>here, then</code>c` later.</p>
Read it slowly. The backslash survived. The code span ended at it, so a\ became the code and b fell out as prose. The backtick meant to close the span opened a new one, so the words "here, then" became code. The c that was meant to be code became prose — and one orphaned backtick was left standing before "later".
One typo, and every boundary after it moves.
This is the second half of a rule that is correct where it is usually taught. The complete Markdown cheat sheet lists the backtick among the characters you escape with a backslash, and in ordinary text that holds: CommonMark says "Any ASCII punctuation character may be backslash-escaped" 8. The same section draws the boundary: "Backslash escapes do not work in code blocks, code spans, autolinks, or raw HTML" 8.
In prose, \` gives you a backtick. Inside code, it gives you a backslash and a broken line.
The spec also says what to do instead. The sentence is worth reading twice: "Backslash escapes are never needed, because one can always choose a string of n backtick characters as delimiters, where the code does not contain any strings of exactly n backtick characters." 3
One backtick, or a run of backticks, in inline code
Count the longest run of backticks inside your code, then wrap the code in a run of a different length — in practice, one longer. A code span closes only on a backtick run of exactly the opening length, so a two-backtick wrapper passes every single backtick inside it through as text. Add one space inside each end.
The rule that makes this work is short: "A code span begins with a backtick string and ends with a backtick string of equal length." 3 A run of one backtick cannot close a span opened with two. A run of three cannot close a span opened with four.
Gruber wrote the same fix into the original syntax: "To include a literal backtick character within a code span, you can use multiple backticks as the opening and closing delimiters" 2. The pandoc User's Guide is shorter still: "If the verbatim text includes a backtick, use double backticks" 9.
The three common shapes, as you would type them:
Wrap it: `` `a` `` done.
A lone backtick: `` ` `` done.
Inline fence: ```` ``` ```` done.
The first renders the code `a` with both backticks visible. The second renders a single backtick as code. The third renders three backticks as inline code — the way you would mention a fence in a sentence without starting one.
All three rendered the same code in all ten modes and all six engines 10.
"Longer" is the habit; the rule is only "different". A span opened with one backtick can hold a run of three, because a run of three never matches a closer of one. Most people find "one longer than the longest run inside" easier to apply without counting twice.
One trap remains. A run of backticks next to the code's own backtick merges into one longer run — which is why the space is not decoration.
When the code starts or ends with a backtick, the space is required
If your code begins or ends with a backtick, put one space between the delimiter and the code. Without it, the code's backtick fuses with the delimiter into a longer run, and the span never opens. CommonMark removes exactly one space from each end, so the space never shows in the output. Add it every time.
The spec's wording: "If the resulting string both begins and ends with a space character, but does not consist entirely of space characters, a single space character is removed from the front and back. This allows you to include code that begins or ends with backtick characters" 3.
Gruber had the same idea in 2004: "The backtick delimiters surrounding a code span may include spaces — one after the opening, one before the closing. This allows you to place literal backtick characters at the beginning or end of a code span" 2.
Leave the space out and the engines split three ways. This line was the test 11:
Try ```a`` here.
The seven CommonMark-family modes — GitHub in both modes, commonmark.js, markdown-it, micromark with GFM, and pandoc's gfm and commonmark readers — rendered it as plain text, with no code at all. Python-Markdown, with and without its fenced-code extension, rendered code reading `a. Pandoc's own markdown reader rendered a loose backtick followed by code reading a.
Three outcomes from one line. None of them is a bug: each engine is following its own documented rule. The spaced version — `` `a` `` from the previous section — gave the same result in all ten.
The padding itself is a second, smaller split. With two spaces on each side, as in `` x ``, the seven CommonMark-family modes removed one space from each end and kept the rest. Pandoc markdown and both Python-Markdown modes removed all of them 11. Pandoc documents this: "(The spaces after the opening backticks and before the closing backticks will be ignored.)" 9
The portable habit follows from both results. Use exactly one space, always — and never rely on extra spaces at the edge of inline code to show up.
A code block inside a code block: lengthen the outer fence
A fenced block ends at the first bare fence line of the same character that is at least as long as the one that opened it. A three-backtick block therefore cannot contain a three-backtick closing line. Open the outer block with four backticks, or with three tildes, and every three-backtick line inside it stays text.
This is the AI-answer case. A README that contains an install command, wrapped in one more fence so it can be pasted, looks like this:
```markdown
# README
```bash
npm install
```
after
```
Here is what happens to it, in order 12. The ```bash line survives as text, because a closing fence may carry nothing after it except spaces or tabs — a language tag disqualifies it 13. The bare fence after npm install does qualify, so it closes the outer block. The word "after" falls out as an ordinary paragraph.
Then the last fence opens a new block with nothing in it.
In seven of the nine fence-capable modes, that last line became an empty code block — a grey box at the bottom of the note with nothing inside. Pandoc's markdown reader and Python-Markdown with its fenced-code extension printed it instead as text at the end of the paragraph, reading "after" followed by three backticks 12.
Either way, the README is cut in half. And when nothing closes that last fence, it can do worse than leave a box: an unclosed fence pulls everything below it into the code block. That is also why an editor has to reparse more than the paragraph you typed in whenever a fence opens or closes.
The spec gives the rule that fixes it: "The closing code fence must be at least as long as the opening fence" 13. A three-backtick line is not at least as long as a four-backtick opener, so it cannot close one:
````markdown
# README
```python
print(1)
```
after
````
GitHub's own documentation gives the same instruction: "To display triple backticks in a fenced code block, wrap them inside quadruple backticks." 14 Pandoc's guide generalises it: "If the code itself contains a row of tildes or backticks, just use a longer row of tildes or backticks at the start and end" 15.
The tilde version works for a different reason. A tilde fence can only be closed by tildes — "(Tildes and backticks cannot be mixed.)" 13 — so no backtick line of any length can end it:
~~~markdown
# README
```python
print(1)
```
after
~~~
Both outer fences kept the inner block as text and kept "after" inside, in nine of ten modes 5. The tenth was Python-Markdown with default extensions, which does not recognise fenced blocks in any form.
Three levels deep follows from the same rule — five backticks outside four outside three, or tildes outside four backticks outside three. That depth was not part of the test; it is the spec rule applied twice.
A backslash fails here too. Writing the inner fence as \`\`\`bash kept the outer block intact, but the backslashes were printed inside it, in all nine fence-capable modes 16. The reader copies a command that will not run.
Where the six engines disagree
On every fix in this reference, the engines agree. They split only on the mistakes: an unspaced span that starts with a backtick, extra padding spaces, and what a stray closing fence turns into. Pandoc's own markdown reader and Python-Markdown follow their own documented rules in those cases — they are separate dialects, not broken parsers.
The seven CommonMark-family modes returned identical results on all ten test cases. The table groups them as one column 4.
| Case | 7 CommonMark-family modes | pandoc markdown | Python-Markdown (default · fenced_code) |
|---|---|---|---|
| A — word in backticks, spaced | code with backticks | same | same · same |
| B — lone backtick, spaced | one backtick as code | same | same · same |
| C — backslash inside a span | backslash printed, line reshuffled | same | same · same |
| D — three backticks, inline | three backticks as code | same | same · same |
| E — no space, starts with a backtick | plain text, no code | loose backtick, then code | code starting with a backtick · same |
| F — two spaces of padding | one space removed each side | all removed | all removed · all removed |
| G — same-length nested fence | closes early, empty block at end | closes early, stray fence as text | no fenced blocks · closes early, stray fence as text |
| H — four-backtick outer fence | works | works | no fenced blocks · works |
| I — tilde outer fence | works | works | no fenced blocks · works |
| J — backslash before inner fence | backslashes printed | same | no fenced blocks · backslashes printed |
Pandoc states its general position directly: "Note that backslash-escapes (and other Markdown constructs) do not work in verbatim contexts" 9. That sentence is why row C is identical across every column, including the two dialects.
Read the table the other way and the practical advice falls out. If a habit gives the same answer in all ten modes, it is portable. The spaced inline wrapper and the longer or tilde outer fence pass that test. The unspaced wrapper and padding-for-effect do not.
Two limits deserve stating plainly. Each case is one input, chosen to mirror the spec's own examples; the normative text is what generalises it. And desktop editors such as Obsidian and VS Code were not run here — if your notes live in one, paste the ten test lines into a note and look.
Neighbouring cases this page does not cover
Three related problems have their own pages. A pipe character inside inline code in a table follows table rules, not code-span rules. An unclosed fence changes every block below it. And how a code block should look is a typography question, separate from how to write one. Each is linked below.
Tables first. Inside a pipe table, a pipe in a code span follows the table's rules, and engines disagree about whether its escape survives — that case is covered in what fits in a Markdown table cell.
Unclosed fences, in depth, are in reparse the paragraph, not the document.
Why snippets belong in fenced blocks at all, and why the opening fence should name its language, is in code snippets as plain-text notes. One detail from there matters here: a backtick fence's language tag cannot itself contain a backtick, so the tag is never a place to hide one.
The look of the block — monospace, line height, wrapping — is in the typography of a code block inside your notes.
How the test was run
Ten short Markdown inputs, one per case, were each rendered as their own document through six engines in ten modes on 30 October 2026. The engines were GitHub's Markdown API, commonmark.js, markdown-it, micromark with GFM, pandoc and Python-Markdown. Every output was saved, and the GitHub calls were re-run later the same day with identical results.
The ten modes, exactly 4:
- GitHub's REST Markdown endpoint in
gfmmode (how comments render) andmarkdownmode (how files and READMEs render) - commonmark.js 0.31.2
- markdown-it 15.0.3, default settings
- micromark 4.0.3 with its GFM extension 3.0.0
- pandoc 3.13 with three readers:
markdown,gfmandcommonmark - Python-Markdown 3.11.2, once with default extensions and once with the fenced_code extension
GitHub's renderer is a live service, so it is the one result that could drift. A second run of all twenty GitHub calls, about four and a half minutes after the first, matched it byte for byte.
The CommonMark spec quoted throughout is version 0.31.2, dated 28 January 2024 3; the GitHub spec is 0.29-gfm, dated 6 April 2019 6.
FAQ
How does one escape backticks in markdown?
You do not escape them inside code — you change the delimiter. Wrap inline code in a run of backticks longer than any run it contains, with one space inside each end: `` `a` `` renders `a` as code. A backslash inside code is printed literally and ends the span early, in every engine tested.
How to put (in markdown) an inline code block that only contains a backtick character?
Type two backticks, a space, the backtick, a space, and two backticks: `` ` ``. The spaces are removed from the output and a single backtick renders as code. That form gave the same result in all ten renderer modes tested, across GitHub, commonmark.js, markdown-it, micromark, pandoc and Python-Markdown.
How do I escape three backticks surrounded by a codeblock in Markdown?
Do not escape them. Open and close the surrounding block with four backticks, or with three tildes, and the inner three-backtick lines stay as text. A backslash before each backtick keeps the block intact but prints the backslashes, so a reader who copies the code gets something that will not run.
How to type a grave accent (backtick) in Markdown?
In ordinary text, a backslash works: \` prints one backtick, because any ASCII punctuation character may be backslash-escaped outside code. Inside inline code, a backslash does not work — wrap the code in two backticks with a space inside each end instead. The two rules apply to different places in the same line.
How to render triple backticks as inline code block in Markdown?
Wrap them in four backticks with a space inside each end: ```` ``` ````. The result is three backticks shown as inline code, without starting a fenced block. It rendered identically in all ten modes tested. Two backticks on each side also satisfies the spec, because the code contains no run of exactly two.
How can I escape 3-backticks code block in 3-backticks code block?
You cannot with the same length. A three-backtick closing line ends any three-backtick block, so the outer block closes early and the last fence opens an empty one. Make the outer fence four backticks — or use tildes — because a closing fence must be at least as long as the opening one.
Inside code, Markdown stops listening to backslashes. It never stops counting backticks — so count them too.
Code, commands and Markdown about Markdown, kept as plain-text notes on your own device — mnmnote.com.
References
Footnotes
-
Stack Overflow, via the Stack Exchange API. Six questions: "How does one escape backticks in markdown?" (q/24313204, 30,852 views, 104 votes, asked 2014-06-19); "How to put (in markdown) an inline code block that only contains a backtick character" (q/55586867, 30,628 views); "How do I escape three backticks surrounded by a codeblock in Markdown?" (q/31825237, 11,704 views); "How to type a grave accent" (q/31048570, 8,742 views); "How to render triple backticks as inline code block in Markdown?" (q/33224686, 7,682 views); "How can I escape 3-backticks code block in 3-backticks code block?" (q/49267811, 7,012 views). Total 96,620 views, as of 2026-10-30. https://api.stackexchange.com/2.3/questions/24313204;55586867;31825237;31048570;33224686;49267811?site=stackoverflow ↩
-
John Gruber, "Markdown: Syntax," Daring Fireball, 2004, section "Code." https://daringfireball.net/projects/markdown/syntax#code — accessed 2026-10-30. ↩ ↩2 ↩3
-
John MacFarlane, CommonMark Spec, version 0.31.2 (2024-01-28), §6.1 "Code spans." https://spec.commonmark.org/0.31.2/#code-spans — accessed 2026-10-30. ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
Own measurement, 2026-10-30. Ten cases, each rendered as its own document. GitHub REST
POST /markdowningfmandmarkdownmodes; commonmark.js 0.31.2; markdown-it 15.0.3; micromark 4.0.3 with micromark-extension-gfm 3.0.0; pandoc 3.13 withmarkdown,gfmandcommonmarkreaders; Python-Markdown 3.11.2 with default extensions and withfenced_code. GitHub calls re-run the same day: 20 of 20 outputs identical. ↩ ↩2 ↩3 -
Own measurement, 2026-10-30, cases H and I. A four-backtick outer fence and a tilde outer fence each kept an inner three-backtick block as text and the following line inside, in 9 of 10 modes. Python-Markdown with default extensions rendered no fenced block. ↩ ↩2
-
GitHub, GitHub Flavored Markdown Spec, version 0.29-gfm (2019-04-06), §6.1 "Code spans." https://github.github.com/gfm/#code-spans — accessed 2026-10-30. ↩ ↩2
-
Own measurement, 2026-10-30, case C. Input with a backslash before a backtick inside a code span; the same result in 10 of 10 modes (GitHub's
gfmmode adds a class attribute to each code tag): backslash printed, span closed at it, following words turned into code, one stray backtick left as text. ↩ -
John MacFarlane, CommonMark Spec, version 0.31.2 (2024-01-28), §2.4 "Backslash escapes." https://spec.commonmark.org/0.31.2/#backslash-escapes — accessed 2026-10-30. ↩ ↩2
-
pandoc User's Guide, "Verbatim." https://pandoc.org/MANUAL.html#verbatim — accessed 2026-10-30. ↩ ↩2 ↩3
-
Own measurement, 2026-10-30, cases A, B and D. A word wrapped in single backticks inside a double-backtick span; a lone backtick inside a double-backtick span; three backticks inside a four-backtick span. Each gave the intended code in 10 of 10 modes, across all six engines. ↩
-
Own measurement, 2026-10-30, cases E and F. Unspaced span starting with a backtick: plain text in 7 of 10 modes, code in pandoc
markdownand both Python-Markdown modes, with different content. Two spaces of padding: one space removed each side in 7 of 10; all removed in pandocmarkdownand both Python-Markdown modes. ↩ ↩2 -
Own measurement, 2026-10-30, case G. A three-backtick block containing a three-backtick block: closed early in 9 of 9 fence-capable modes; the final fence became an empty code block in 7 of 9 and text in a paragraph in pandoc
markdownand Python-Markdown withfenced_code. Python-Markdown with default extensions rendered no fenced block. ↩ ↩2 -
John MacFarlane, CommonMark Spec, version 0.31.2 (2024-01-28), §4.5 "Fenced code blocks." https://spec.commonmark.org/0.31.2/#fenced-code-blocks — accessed 2026-10-30. ↩ ↩2 ↩3
-
GitHub Docs, "Creating and highlighting code blocks." https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks — accessed 2026-10-30. ↩
-
pandoc User's Guide, "Fenced code blocks." https://pandoc.org/MANUAL.html#fenced-code-blocks — accessed 2026-10-30. ↩
-
Own measurement, 2026-10-30, case J. Inner fence written with a backslash before each backtick, inside a three-backtick block: backslashes printed in 9 of 9 fence-capable modes. ↩