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?

up vote 268 down vote accepted

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: Extra spacing 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 2 closing backticks instead of the final two closing backticks.

``foo` `` produces foo`
`` `foo`` produces `foo
`` ` `` produces single backtick `
  • 1
    How do you have two consecutive backticks in inline code? – asmeurer Jun 16 '13 at 16:44
  • 3
    @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 Jun 16 '13 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 Jun 5 '14 at 14:55
  • 5
    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!) – Steve Chambers Jan 25 '16 at 21:05

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)

  • 6
    Thanks for pointing out that extra spaces are required! – CoDEmanX Aug 28 '14 at 20:40
  • 8
    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 Mar 3 '15 at 18:53
  • 1
    A very cool trick; first time I hear about it! – mgol Sep 5 '16 at 22:13
  • 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 Apr 24 at 13:31

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

  • 1
    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 Jul 16 '15 at 9:44
  • @TWiStErRob - The escape works in the codeblock. For example, List`1 is achieved using `List\`1`. – Mike Mar 5 '16 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 Mar 5 '16 at 16:36
  • 1
    @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 Mar 5 '16 at 16:54

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
```
  • Thanks I was looking for that – Brice Feb 9 at 11:03
  • This should be the accepted answer. – quemeful Sep 17 at 23:40

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 this only works in SE questions & answers, not comments; HTML entities are displayed as plaintext in comments.

You must log in to answer this question.

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