Discord Code Blocks: Inline Code, Multi-Line Blocks and Syntax Highlighting
One backtick for inline code, three for a block, and a language name after the opening backticks for colors. Covers mobile, pasting backticks inside code, the 2,000 character limit and what code blocks switch off.
Discord has two kinds of code formatting, and both use the backtick character (`):
`inline code` inside a sentence
```
a multi-line
code block
```
Both are in Discord's Markdown Text 101 help article: wrap text in single backticks for a one-line code span, and in three backticks for a multi-line block. Text inside is shown in a monospace font on a darker background, exactly as typed.
Quick reference
| You want | You type |
|---|---|
| Code inside a sentence | `npm install` |
| A block of several lines | Three backticks, your lines, three backticks |
| Colored syntax | Three backticks followed directly by a language name, such as js or python |
| A backtick inside inline code | Use two backticks as the wrapper: `` a ` here `` |
| Hidden code (spoiler) | Not possible. Discord states that spoilers inside code blocks are not hidden. |
The backtick is on the key to the left of 1 on a US keyboard. On an iPhone, tap 123 and hold the apostrophe key to get it. On Gboard, tap ?123, then =\<.
Multi-line code blocks
Put three backticks on their own line, then the code, then three backticks again:
```
SELECT name, email
FROM users
WHERE active = true;
```
On desktop, pressing Enter sends the message. Use Shift + Enter to add a new line while you type the block.
You can also keep a short block on one line: ```like this```. It still gets the block background, which is wider than inline code.
Syntax highlighting
Add a language name right after the opening backticks, with no space, then start the code on the next line:
```python
def greet(name):
return f"Hello, {name}"
```
Discord's help article does not list the languages it accepts. These are names that work in practice and cover most uses:
| Type this | For |
|---|---|
js, ts, jsx |
JavaScript and TypeScript |
python or py |
Python |
json, yaml, xml, html, css |
Data and markup |
bash or sh |
Shell commands |
sql |
SQL |
java, cs, cpp, c, go, rust, php, rb, lua |
Other common languages |
diff |
Lines starting with + show green, lines starting with - show red |
md |
Markdown source |
ansi |
Colored text with terminal escape codes |
Two things to know:
- An unknown name is not an error. The block just renders without colors. If your code is not highlighted, check the spelling of the language.
- A first line that is one word gets eaten. If a single word sits directly after the opening backticks and the rest is on the following lines, Discord reads that word as the language name and does not show it. Start your content on a new line.
The diff and ansi blocks are how people get colored text on Discord, since there is no color syntax for normal messages. The Discord Colored Text Generator builds an ansi block for you. Note that Discord does not document ansi blocks, so test before relying on them.
What a code block turns off
Inside backticks, Discord stops interpreting everything else. That is useful and occasionally surprising:
| Inside a code block | What happens |
|---|---|
**bold**, ~~strike~~, __underline__ |
Shown literally, with the symbols |
||spoiler|| |
Not hidden (Discord says so in its Spoiler Tags article) |
<t:1796148000:F> timestamps |
Shown as the raw code |
[text](url) masked links |
Shown as typed |
Emoji codes like :smile: |
Shown as text |
This makes a code block the easiest way to show someone how to format something. Want to teach a friend spoilers? Send `||text||` in inline code.
It is also the standard way to post a table. Discord does not render Markdown tables, so the pipes and dashes show as plain text and the columns do not line up. Inside a code block the font is monospace, so an aligned table keeps its shape. The Discord Text Formatter has a "Clean up pasted Markdown" button that converts a Markdown table (from ChatGPT, for example) into an aligned code block.
Long code and the character limit
Discord's Sending Messages article gives the cap as 2,000 characters per message, or 4,000 with Nitro. The backticks and the language name count. When a paste is too long:
- Let Discord attach it: the same article says messages over the cap "will be converted into a text file", which readers can expand in chat.
- Split it: close the block with three backticks at a sensible line, then open a new block in the next message. Each message needs its own opening and closing backticks.
- Link it: for anything longer than a screen, a gist or paste link is easier to read than five messages.
When it does not work
| Problem | Cause | Fix |
|---|---|---|
| Backticks show as plain characters | You typed apostrophes (') or a different quote character, or a phone keyboard substituted smart quotes |
Use the real backtick. On iPhone, hold the apostrophe key. |
| The block never ends and swallows the rest of the message | Missing closing backticks | Add three backticks on their own line at the end |
| No colors | Misspelled language, a space after the backticks, or code on the same line as the language | Opening line must be exactly three backticks plus the language, then a line break |
| Code contains three backticks (for example a Markdown file) | The inner backticks close the block early | Upload it as a file, or replace the inner fence while posting |
| Extra blank line at the top | A blank line after the opening backticks | Start the code on the line right after the opening line |
More Discord formatting
- Discord text formatting cheat sheet: every option on one page
- How to strikethrough on Discord
- Discord small text and big text
- Discord Spoiler Text Generator and Discord Timestamp Generator
- General Markdown: code blocks and language names