731

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?

5
  • 14
    See meta.stackexchange.com/questions/55437/…
    – ChrisF Mod
    Commented Mar 11, 2011 at 14:29
  • 11
    with a \ Example: ` Commented Oct 7, 2013 at 9:25
  • 21
    @JohannesKuhn `\` doesn't work most of the time in inline code
    – phuclv
    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.
    – Kvothe
    Commented Mar 8, 2023 at 14:33
  • I resolved with `` ` ``.
    – Pine Code
    Commented Aug 2, 2023 at 7:53

6 Answers 6

693

Use four spaces before your code:

List`1

Or use double backticks: ``List`1`` produces List`1.

See http://daringfireball.net/projects/markdown/syntax, linked to from the formatting question box.

Note: Putting a single space on each side of the text will be necessary if you want to have a backtick at the beginning or the end of your code, e.g., foo`, `foo, or just `. This will keep it from consuming the first two closing backticks instead of the final two closing backticks.

`` foo` `` produces foo`
`` `foo `` produces `foo
`` ` `` produces single backtick `

You can put the space just on the side ending with a backtick, but then that space might get included in the in-line code (in GitHub Flavoured Markdown for example) which is why using a single space on each side of the code is generally preferred.

If you need to show extra backticks, enclose them with a higher number of them:

``` ``two backticks`` ``` produces ``two backticks``
11
  • 4
    How do you have two consecutive backticks in inline code?
    – asmeurer
    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 !``!```!
    – Brian
    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.
    – user163250
    Commented Jun 5, 2014 at 14:55
  • 20
    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?
    – wisbucky
    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 :)
    – Patronics
    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.
    – mmseng
    Commented Sep 9, 2023 at 5:55
203

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)

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 ````)
    – Jedidja
    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 :-/).
    – PhiLho
    Commented Apr 24, 2018 at 13:31
  • Seems this does not work in comments on a pull request, unfortunately.
    – oligofren
    Commented Jan 21, 2022 at 12:19
  • 1
    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](http://www.google.com)

# not h1

## not h2

### not h3

+ not ul

- not ul

4
  • 2
    I think the escape part of your answer is irrelevant since the OP is asking about how to escape inside an inline code block.
    – TWiStErRob
    Commented Jul 16, 2015 at 9:44
  • @TWiStErRob - The escape works in the codeblock. For example, List`1 is achieved using `List\`1`.
    – Mike
    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!
    – Mike
    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.
    – TWiStErRob
    Commented Mar 5, 2016 at 16:54
63

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
```
4
  • 3
    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!
    – stevec
    Commented Apr 18, 2020 at 14:56
  • 1
    This worked for me in BitBucket after trying a lot of the other suggestions!
    – CodeMonkey
    Commented Nov 11, 2020 at 15:32
  • This also works fine on GitLab and is probably the simplest solution. Commented Jul 22 at 10:58
19

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.

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?"
    – oligofren
    Commented Jan 21, 2022 at 12:22
14

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): ```

3
  • 1
    ` backtick for comments seem to work, thanks Commented May 4, 2020 at 0:46
  • 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>
    – Christian
    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``.
    – wjandrea
    Commented Jul 3, 2023 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .