756

How do I escape a backtick ` within a code block?

This is probably a duplicate, since I'm sure it's a common concern, but I can't find a question that addresses this specifically.

How do I write List'1 with the "1" character still in the code-text format?

dx_over_dt
8695 silver badges12 bronze badges
asked Mar 11, 2011 at 13:58
5
  • 14
    See meta.stackexchange.com/questions/55437/… Commented Mar 11, 2011 at 14:29
  • 12
    with a \ Example: ` Commented Oct 7, 2013 at 9:25
  • 22
    @JohannesKuhn `\` doesn't work most of the time in inline code Commented Jun 16, 2018 at 13:15
  • 1
    Escaping with `` does not seem to work at all (at least for inline code). It seems that NOT escaping but using double ticks to start (end) the inline code does work. Commented Mar 8, 2023 at 14:33
  • I resolved with `` ` ``. Commented Aug 2, 2023 at 7:53

6 Answers 6

720

For reference, see http://daringfireball.net/projects/markdown/syntax, which is linked in the formatting question box.

Inline code

For simple cases, use double backticks: ``List`1`` produces List`1.

To put one or more backticks at the beginning or the end of your code, e.g., foo`, `foo, or just `, add a space on each side as well:

  • `` foo` `` produces foo`
  • `` `foo `` produces `foo
  • `` ` `` produces `

Without these spaces, parsing breaks:

  • ``foo``` produces ``foo```
  • ```foo`` produces ```foo``
  • ````` produces `````

Space is needed on both sides, even if a backtick only appears on one side of the intended code. Otherwise, the space itself may be considered part of the code span:

  • ``foo` `` produces foo`
  • `` `foo`` produces `foo

If the inline code must contain a longer sequence of backticks, use a backtick sequence at the start and end that is longer than anything in the code itself:

  • ```foo``bar``` produces foo``bar
  • ````foo```bar```` produces foo```bar

And so on. You can also see this technique in action in the source code of this answer.

Code blocks

Code blocks can be created either using "code fences" (lines before and after with three or more backticks), or by indenting each line of the code by an additional four spaces (above and beyond the code's indentation, as well as any indentation needed to put it inside a bullet-point list etc.).

The extra-indentation approach makes it simple to include backticks in the code block, but can otherwise be unwieldy.

With code fences, there is usually no problem with putting backticks in the code:

` this works
this also works: ```
``` it even works with text here

However, this breaks if the code block must have three backticks on a line by themselves, even with leading and/or trailing spaces. In this case, we again fix the problem by using a longer sequence of backticks on the start and end lines. For example, source like

````
```
````

produces

```
Karl Knechtel
8,2833 gold badges34 silver badges56 bronze badges
answered Mar 11, 2011 at 14:20
11
  • 4
    How do you have two consecutive backticks in inline code? Commented Jun 16, 2013 at 16:44
  • 9
    @asmeurer: Wrap your inline code with triple backticks. !``! For triple or higher backticks, you can wrap your inline code with double backticks, rather than quadruple backticks (unless you need both triple and double backticks at different places). !```! vs !``!```! Commented Jun 16, 2013 at 19:09
  • 2
    As mentioned in the other answers to the question, you can simply escape backticks with a backslash `\` for inline formatting. Commented Jun 5, 2014 at 14:55
  • 22
    Extra spacing also required if you want a backtick at the start of your quote. I just wanted a backtick and nothing else in this answer so used `` ` `` (couldn't work out how to quote this in this comment though!) Commented Jan 25, 2016 at 21:05
  • 3
    sheesh, why can't inline/post markdown parsing be consistent with the comment markdown parsing? Commented Jul 10, 2019 at 22:29
  • 1
    @user163250 backsslash does not work inside a backticked section, it just renders as a backslash Commented Nov 21, 2019 at 12:30
  • 1
    Four-spaces which works for me (Redmine markdown) Commented Dec 28, 2020 at 7:28
  • someone 100 years in the future will write a Concise History of Character Escaping, and it will be a top-seller. Commented Dec 7, 2021 at 21:17
  • using 4 backticks to escape sequences of 3 backticks worked perfectly for me :) Commented Feb 12, 2023 at 22:58
  • For Stack Overflow comments, I had to use the \` alternative Commented Sep 8, 2023 at 18:24
  • Using double-backticks to enclose the entire block when it needs to contain backticks is what worked for me on github.com MD files. I find that to be an unintuitive and backwards convention, but whatever works I guess. Commented Sep 9, 2023 at 5:55
205

For GitHub, like for displaying a MySQL `table_name`, in regular text use \` (backslash backtick).

For showing backticks inside inline codeblocks `table_name`, use double backticks with extra spaces `` `table_name` `` around the inner single backticks.

To show the previous example explanation in an inline codeblock: `` `table_name` ``, surround the whole in three backticks with extra spaces, e.g. ``` `` `table_name` `` ```

(head a splode)

This_is_NOT_a_forum
6,5454 gold badges38 silver badges55 bronze badges
answered Nov 11, 2012 at 13:21
4
  • 13
    And that continues for any number of backticks. If you are writing about how to use Prism syntax highlighting and want to display three backticks, you need to "escape" them with four backticks and a space. (i.e. ```` ```language_javascript ````) Commented Mar 3, 2015 at 18:53
  • An amusing secondary quest would be to display "symbol followed (or preceded) by space in inline code"... Eg. I want to represent the + and space sequence, apparently it is not possible (no-break space is collapsed as well :-/). Commented Apr 24, 2018 at 13:31
  • Seems this does not work in comments on a pull request, unfortunately. Commented Jan 21, 2022 at 12:19
  • 2
    The double backticks with extra spaces trick doesn't work in comments :( Example: `` table_name `` Commented Jun 10, 2022 at 13:18
84

You can use an extra backtick at the start and end to make sure it escapes correctly:

``List`1``

When inline it will display as List`1.

Markdown provides backslash escapes for the following characters:

\ backslash
` backtick
* asterisk
_ underscore
{} curly braces
[] square brackets
() parentheses
# hash mark
+ plus sign
- minus sign (hyphen)
. dot
! exclamation mark

for example, this:

## \\ \` \* \_ \{ \} \[ \] \( \) \# \+ \- \. \!

returns:

\ ` * _ { } [ ] ( ) # + - . !

Escaped codeblock, italics, bold, link, headings and list:

`not codeblock`, *not italics*, **not bold**, [not link](https://www.example.com)

# not h1

## not h2

### not h3

+ not ul

- not ul

answered Sep 25, 2013 at 8:20
4
  • 3
    I think the escape part of your answer is irrelevant since the OP is asking about how to escape inside an inline code block. Commented Jul 16, 2015 at 9:44
  • @TWiStErRob - The escape works in the codeblock. For example, List`1 is achieved using `List\`1`. Commented Mar 5, 2016 at 16:33
  • @suhail - The escape was the only method I could get to work for this: grep "docker-machine-`uname -s`-`uname -m`", so thanks! Commented Mar 5, 2016 at 16:36
  • 2
    @Mike I'm not even sure what I meant in retrospect, I was probably referencing the second half of the answer, because that's plain text, not code block (notice: no gray background). To clarify: the "put more ticks around the code than you want to escape" part of the answer is awesome, but the backslash-escape sequences are not useful within code blocks as I tried. Commented Mar 5, 2016 at 16:54
66

For fenced code blocks, the prescription is more backticks.

This Markdown

````
```
Example of Markdown syntax for fenced code block with triple back ticks
```
````

will be rendered like this

```
Example of Markdown syntax for fenced code block with triple back ticks
```
answered Sep 13, 2017 at 15:18
4
  • 4
    Another trick that works when wanting to quote a tripple backtick within a tripple backtick section, is to insert a ZWJ (Zero-Width-Joiner) character between two of the three backticks. Other zero-width unicode characters probably works as well. Commented Jul 13, 2019 at 20:10
  • 2
    This was the most helpful answer. Two other things: i) same technique works in github flavoured markdown. and ii) to take regular code and add four spaces as the start of every line, in sublime text (or other code editors), find and replace ^ (start of a line) with ^ (start of a line followed by four spaces). Works a treat! Commented Apr 18, 2020 at 14:56
  • 1
    This worked for me in BitBucket after trying a lot of the other suggestions! Commented Nov 11, 2020 at 15:32
  • This also works fine on GitLab and is probably the simplest solution. Commented Jul 22, 2024 at 10:58
20

Use <code> and HTML character entities instead. This is especially useful in situations where your code is already wrapping things in backticks (e.g., in Swift, enum { case `true` = 1 }).

HTML gives you three ways to specify a character entity for a backtick: &grave;, &#96;, & &#x60;. The choice is yours.

The way to accomplish the example with this technique would be <code>List&grave;1</code>, resulting in List`1. The markdown for List`List`1 would just be <code>List&grave;List&grave;1</code>. It's a little messy, but not unbearable if you're used to reading/writing HTML.

While more cumbersome than normal Markdown, this approach is also much more compatible. Some Markdown parsers don't like backslash-escapes; some will created nested code spans instead of working like @Brian's example; others will do other weird things; but the HTML approach will only fail if the Markdown parser has HTML turned completely off (which is exceedingly rare).

The general approach I take is "If I can't do it with Markdown, or if doing it in Markdown is a complicated mess of repeated symbols and tricks, just drop down to HTML for that use-case."


Note that on SE sites, this only works in questions & answers, not comments (unfortunately! and somewhat negating my "exceedingly rate" analysis above!); HTML entities are displayed as plaintext in comments.

answered Jul 5, 2016 at 1:08
1
  • 1
    This is the only thing that works with comments on code in pull requests in github. As in "What does <code>&grave;in&grave;</code> mean here?" Commented Jan 21, 2022 at 12:22
15

It's different in posts and in comments.

For posts

1.

`` ` ``

Result (only for posts, not valid in comments): `

2.

<code>\`</code>

Result (only for posts, not valid in comments): `

For comments

`\``

Result (only for comments, not valid in posts): ```

answered May 1, 2020 at 11:15
2
  • 1
    Adding my 2 cents: the only way I got this to work in PhpStorm markdown editor and preview, and bitbucket markdown preview is to use <code>\`xx\`</code> Commented Aug 9, 2022 at 8:44
  • For comments, leading and trailing backticks both need to be escaped. `\`x\`` renders as `x`. If you only escape the first one, you get ``x``. Commented Jul 3, 2023 at 19:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.