How to Send Code Blocks in Discord (With Syntax Highlighting)
Contents
To send a code block in Discord, wrap your code in three backticks (```) on their own lines. Add a language name right after the opening backticks to get syntax highlighting:
A code block with syntax highlighting
Type the opening ``` plus the language, press Shift + Enter for a new line (plain Enter sends the message), paste your code, then close with ``` on its own line.
```js
function greet(name) {
return `Hello, ${name}!`;
}
```
The backtick is the key under Esc on US and UK keyboards. On many European layouts it’s a dead key next to Backspace: press it, then Space. On a phone, long-press the apostrophe key.
Inline code
For a single command, function or file name inside a sentence, use one backtick on each side. It’s shown in a monospace font, and Discord won’t turn its contents into links, mentions or formatting.
Inline code
Run `npm install` first, then open `src/index.js`.
Code that contains a backtick
Wrap inline code in double backticks when the code itself contains one, with a space inside the delimiters. For blocks, the triple backticks already take care of it.
`` const msg = `Hi ${name}`; ``
Syntax highlighting languages
Discord uses highlight.js, so it recognizes almost every language. The name goes right after the backticks, with no space. These are the ones developers use most:
| Language | Write |
|---|---|
| JavaScript | ```js or ```javascript |
| TypeScript | ```ts |
| Python | ```py or ```python |
| HTML / XML | ```html |
| CSS / SCSS | ```css / ```scss |
| JSON | ```json |
| Shell | ```bash or ```sh |
| PHP, Java, C#, C++, Go, Rust, SQL | ```php, ```java, ```cs, ```cpp, ```go, ```rust, ```sql |
| Lua (Roblox scripts) | ```lua |
| YAML, Markdown | ```yaml, ```md |
Colored text with diff
The diff language colors lines that start with + green and lines that start with - red. It’s perfect for showing what you changed in someone’s code, and people also use it just to color a message.
```diff
- const total = price * 1.2;
+ const total = price * (1 + TAX_RATE);
```
Code block not working?
- A space after the backticks.
``` jsbreaks highlighting. Write```js. - Code on the same line as the opening backticks. Everything on the first line after
```is read as the language name. Start the code on the next line. - Apostrophes instead of backticks.
'''isn’t the same character as```. Check the key. - Missing closing backticks. The rest of the message becomes code. Close with
```on its own line. - Unknown language name. The block still shows as code, just without colors. Try the full name, like
javascript.
Code longer than the message limit
Discord messages are limited to 2,000 characters (4,000 with Nitro). For longer code, don’t split it across messages: drag the file into the chat, or paste it and let Discord turn it into a file attachment with a preview. For a whole project, share a link to a GitHub repository or Gist instead, so people can read it with line numbers.
Format code before you paste it
Code that’s indented properly gets answered faster on help servers. Run it through your editor’s formatter first (Shift + Alt + F in VS Code on Windows, Shift + Option + F on macOS), include the full error message in its own code block, and say what you expected to happen. When you’re asking about an error, the console output is often more useful than the code.
FAQ
How do I make a code block in Discord on mobile?
The same way: three backticks, the language, a new line, your code, and three backticks on the last line. Long-press the apostrophe key to find the backtick on most phone keyboards.
Can I send a code block without syntax highlighting?
Yes. Leave out the language name: ```, a new line, your code, and ```.
How do I write three backticks without starting a code block?
Put a backslash before each one: \`\`\`.
Does Discord support line numbers in code blocks?
No. If line numbers matter, attach the file or link to GitHub, where every line has a number and a link.