How to Share Code in Slack and Discord Without Breaking the Formatting

How to Share Code in Slack and Discord Without Breaking the Formatting

Slack and Discord both support code formatting, but it's easy to get wrong: paste code as normal text and you can lose indentation, get curly quotes, or trigger auto-formatting. Paste 200 lines and the whole channel scrolls away. Here's how to share short code properly in each app, and when to use a link instead.

The two kinds of code formatting

Inline codeCode block
Syntax`npm install` (one backtick each side)Three backticks on the lines before and after
Use forA command, a variable name, a file pathSeveral lines of code, logs, JSON

Slack

  1. Type ``` to start a code block, or click the code block button in the formatting toolbar.
  2. Paste your code. Use Shift + Enter for new lines if your settings send on Enter.
  3. Close it with ``` and send.

Discord

  1. Type three backticks followed by the language name, then a new line:
    ```python
    def greet(name):
        return f"Hello, {name}"
    ```
  2. Discord applies syntax highlighting for common languages such as js, python, json, bash and css.
  3. Messages have a length limit, so very long code may be split or turned into a file attachment.

Keep the formatting intact

When to share a link instead

Chat is great for a few lines. For more, a snippet link is better:

Paste the code into the code sharing tool, choose the language, and post the link with one sentence: "Login handler, the null check on line 18 never fires." Details in how to share code snippets online.

Etiquette for code in channels

Sharing a screenshot of a UI problem instead? See sharing screenshots in Slack, Discord and Teams.

Line numbers, raw view, 23 language labels, no account. Paste once, share anywhere.

Share long code as a link

Frequently asked questions

How do I make a code block in Slack?

Type three backticks (```), paste your code, and close with three more backticks. You can also use the code block button in the formatting toolbar.

How do I get syntax highlighting in Discord?

Add the language name right after the opening backticks, for example ```python on the first line. Discord highlights many common languages.

Why do my quotes change in Slack?

Some setups and devices convert straight quotes into curly "smart" quotes outside code formatting. Put code inside backticks so it stays exactly as typed.

How long a code snippet should I paste into chat?

Up to about 15 to 20 lines is comfortable. Beyond that, share a snippet link so the channel stays readable and people can refer to line numbers.

Related articles

← All guides