Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A guide to ANSI on Discord

Messages with colors:

Discord is now slowly rolling out the ability to send colored messages within code blocks. It uses the ANSI color codes, so if you've tried to print colored text in your terminal or console with Python or other languages then it will be easy for you.

To be able to send a colored text, you need to use the ansi language for your code block and provide a prefix of this format before writing your text:

\u001b[{format};{color}m

The \u001b is the unicode for ESCAPE/ESC, see http://www.unicode-symbol.com/u/001B.html. If you want to use it yourself without bots, then you need to copy paste the character from the website.

After you've written this, you can type and text you wish, and if you want to reset the color back to normal, then you need to use \u001b[0m as prefix.

Here is the list of values you can use to replace {format}:

  • 0: Normal
  • 1: Bold
  • 4: Underline

Here is the list of values you can use to replace {color}: The following values will change the text color.

  • 30: Gray
  • 31: Red
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Pink
  • 36: Cyan
  • 37: White

The following values will change the text background color.

  • 40: Firefly dark blue
  • 41: Orange
  • 42: Marble blue
  • 43: Greyish turquoise
  • 44: Gray
  • 45: Indigo
  • 46: Light gray
  • 47: White

Let's take an example, I want a bold green colored text with the very dark blue background. I simply use \u001b[0;40m (background color) and \u001b[1;32m (text color) as prefix. Note that the order is important, first you give the background color and then the text color.

Alternatively you can also directly combine them into a single prefix like the following: \u001b[1;40;32m and you can also use multiple values. Something like \u001b[1;40;4;32m would underline the text, make it bold, make it green and have a dark blue background.

Raw message:

```ansi
\u001b[0;40m\u001b[1;32mThat's some cool formatted text right?
or
\u001b[1;40;32mThat's some cool formatted text right?
```

Result: Background and text color

The way the colors look like on Discord is shown in the image below ^^

Note: If the change as not been brought to you yet, or other users, then you can use other code blocks in the meantime to get colored text. See this gist: https://gist.github.com/matthewzring/9f7bbfd102003963f9be7dbcf7d40e51

ANSI Colors

@kkrypt0nn
Copy link
Author

kkrypt0nn commented Jan 9, 2022

Oh fair enough ^^ I will also edit that :)

@asdwqdasd123sasda
Copy link

asdwqdasd123sasda commented Jan 11, 2022

Hey! i can't do this

@kkrypt0nn
Copy link
Author

kkrypt0nn commented Jan 11, 2022

As guide says:

Discord is now slowly rolling out the ability to send colored messages within code blocks.

Not everyone has access to that feature yet. You will have to be patient.

@AloneMaster
Copy link

AloneMaster commented Jan 11, 2022

Wow ! thanks for making this guide it looks so cool.

@Cupcakee1
Copy link

Cupcakee1 commented Jan 12, 2022

is there a way to write this and for it not to show the slide code on mobile??

@kkrypt0nn
Copy link
Author

kkrypt0nn commented Jan 12, 2022

No there is no way to make it work on mobile. I am running the Discord Canary (Alpha) Mobile Version 11.1 (Build 111201) and syntax highlighting is far behind, as always.
The only languages I got working are Python and JavaScript. All others, including ANSI, are not working and this is nothing new.
You will need to wait some time for it to be visible on mobile.

@DomzyMonz
Copy link

DomzyMonz commented Jan 12, 2022

I just found out you can make the text underlined.
image
It's really buggy, though, the underline got removed when i reloaded the channel, and it underlines every ANSI code block

@kkrypt0nn
Copy link
Author

kkrypt0nn commented Jan 12, 2022

Thanks, will be edited accordingly as soon as possible!

@darksunlight
Copy link

darksunlight commented Jan 13, 2022

It's really buggy, though, the underline got removed when i reloaded the channel, and it underlines every ANSI code block

Appending \u001b[0m to reset the styles after each message seems to prevent this.

@asportnoy
Copy link

asportnoy commented Jan 13, 2022

One thing to clarify is if you're typing it in the app, writing \u001b will not work. You have to actually paste the character.

@revelcw
Copy link

revelcw commented Jan 22, 2022

Is there no way to make it italic?

@CoolingTool
Copy link

CoolingTool commented Jan 23, 2022

I'm just gonna point out that discord uses this https://github.com/discord/SimpleAST for mobile code highlighting. Hasn't been updated for awhile and isn't up to date in the app since typescript doesn't work in canary when it was added back in november

@sevenc-nanashi
Copy link

sevenc-nanashi commented Jan 26, 2022

https://gist.github.com/sevenc-nanashi/67bfed2bdd0758eb20ac9bcd6fd88f84
I've translated into Japanese, please tell me if it's bad.

@TheOnlyWayUp
Copy link

TheOnlyWayUp commented Jan 28, 2022

Huh, I remember commenting on here, did my message get deleted?

@TacosGit
Copy link

TacosGit commented Feb 14, 2022

Wait I cant do this XD

@wooningeire
Copy link

wooningeire commented Feb 22, 2022

Some things (tested in Firefox + Discord Canary):

  • You can specify as many modifiers as you want (but at least 1) in a single escape sequence; you aren't required to specify a {format} alongside a {color}, and Discord does not appear to make the distinction between {format}s and {color}s as described here.
  • Order of the different categories does not matter; you can specify background color and text color and formatting in any order in a single escape sequence, and in any order in separate escape sequences.
    • However, the modifiers are still parsed from first-to-last:
      • If you have multiple text color modifiers in an escape sequence, for instance, only the last one will take effect.
      • If you move around the 0 (reset) modifier, only the modifiers following the 0 will take effect (and the rest will be reset).
  • Should note that all of these modifiers add onto the previous formatting. If you have �[32m hello �[1;42m hi, then hi will have a text color, a background color, and be in bold, while hello will only have a text color.

There is a bug with ANSI codeblocks currently that causes styles from one block to overflow into other blocks. Namely, the last style in the last rendered ANSI codeblock (you can trigger a re-render by having the mouse enter or leave a message containing a codeblock) will be used to render the first part of the next rendered ANSI codeblock. The text editor also experiences this. For people doing stuff with ANSI codeblocks, you might want to start off the message with an escape sequence containing a 0 to keep the message from changing when re-rendered. (The text editor also does not account for differences between Dark mode and Light mode when there is a background color applied.)

@asportnoy
Copy link

asportnoy commented Apr 23, 2022

@AshKetchumPL

The \u001b is the unicode for ESCAPE/ESC, see http://www.unicode-symbol.com/u/001B.html. If you want to use it yourself without bots, then you need to copy paste the character from the website.

@Hamsterland
Copy link

Hamsterland commented Apr 26, 2022

Why the hell isn't this out yet

@asportnoy
Copy link

asportnoy commented Apr 26, 2022

It is

@Hamsterland
Copy link

Hamsterland commented Apr 26, 2022

@asportnoy Give me an example of it being used ina code block then

@asportnoy
Copy link

asportnoy commented Apr 26, 2022

All of the images in this gist

@Hamsterland
Copy link

Hamsterland commented Apr 26, 2022

I am on Canary and it does not work

@kkrypt0nn
Copy link
Author

kkrypt0nn commented Apr 26, 2022

I am on Canary and it does not work

Make sure you copy paste the character correctly from the website when using it without a bot.

Here is the website again http://www.unicode-symbol.com/u/001B.html

@F8ER
Copy link

F8ER commented May 2, 2022

I am on Canary and it does not work

As @kkrypt0nn mentioned:

Note the very first character which may not be visible or represent relatively ambiguous visuals in certain circumstances (e.g. unsupported/custom font/charset). It may represent a rectangle:
...
...
Perhaps it's worth to mentioned that some try experimenting with an HTML (&#x1B) or Unicode (\u001b) encoded character reference instead of the actual "raw" character, though so far it's noticed that Discord processes messages with the latter only.

Source: https://stackoverflow.com/a/72087277/5113030

@hdeviscute
Copy link

hdeviscute commented May 9, 2022

You can combine bold with underlined too, just put the numbers together
Example : 14 = Underlined Bold

@undefynedd
Copy link

undefynedd commented May 13, 2022

This works nicely with the Block Elements in Unicode!

@KoopintheHub
Copy link

KoopintheHub commented May 22, 2022

works for me. that's a really nice feature they added :)

@F8ER
Copy link

F8ER commented May 22, 2022

It's worth to mention that it may not work on certain Discord general mobile clients (e.g. Android), as of today at least (client/app: 126012 (stable)):
20220522_162618_nJNWs

@yoonyeonkeum
Copy link

yoonyeonkeum commented May 22, 2022

@MegaCobblepot83
Copy link

MegaCobblepot83 commented Nov 19, 2022

Copy here: �[0;0m
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment