I noticed that sometimes my code gets highlighted in different colors when rendered.

  • What is syntax highlighting?
  • How does it work?
  • Why isn't my code being highlighted correctly?
  • How do I report a bug or request a new language?
  • How do I use syntax highlighting?
  • What languages are currently available on Stack Exchange?

Return to FAQ index

  • I have question: why SE stuff (one person) will not report further such fundamental bugs? This person will once read all issue list of highlight.js and can easily decide it is bug or not. This looks really non-profesional when you see such highlight bugs... The SE chose that library so I think - it is SE responsibility to report such bugs further to highligt.js - especially In situation when this process is complicated (everybody need to get know all issue list). Am I wrong? – Kamil Kiełczewski Dec 3 '20 at 11:45

What is syntax highlighting?

Syntax highlighting allows code in posts to be highlighted based on the language it's written in, to make it easier to read.

How does it work?

Stack Exchange does not have its own syntax highlighting engine. It uses highlight.js, and may not necessarily be using the latest release of that library. Therefore, any bugs and feature requests regarding syntax highlighting cannot be handled by Stack Exchange.

Syntax highlighting is assigned, based on tags, to the preview when creating or editing posts as soon as you stop typing for 5 seconds.

Why isn't my code being highlighted correctly?

1. Check that the site you're using has syntax highlighting enabled

The full list is available on Meta. If syntax highlighting is not enabled on a site follow the instructions in this question.

2. Check that the language is supported by highlight.js

Look at the list of highlight.js supported languages. If your language is not on the list, it needs to be created within the highlight.js project before it can be deployed by Stack Exchange.

If a language is already on the list but not used on Stack Exchange, please raise a feature request here on Meta to have it deployed on the network.

3. Check that the language is used by Stack Exchange

Look at the highlight-loader.js file on the dev CDN, open in your favourite editor, and search for the language hint. For example, searching for "dart" brings you to a line consisting of hljs.registerLanguage('dart', .... This means that lang-dart is an available language hint. Searching for "psh" or "powershell" doesn't return any results, meaning that psh is not supported by the Stack Exchange version of highlight.js.

4. Check that the tags on the question have syntax highlighting enabled

Behind the scenes, Stack Exchanges uses the tags on the question to determine the language you are using. Each tag has a highlighting language specified in its properties, or no language. If there's more than one tag that has a highlighting language specified, or if none of the tags have one specified, it uses a default and lets highlight.js infer what's the best language to use. The same highlighting language that is used on the question is also used on its answers.

Visit that tag's wiki page. Click a tag (such as ""), then click "Learn more...", and the language hint (if any) that is currently being used for that tag will be displayed at the very bottom, below the buttons for the wiki:

Code language (used for syntax highlighting): lang-java

If the tag doesn't have a highlighting language specified when it should, create a on the per-site meta for your site to request it. (Only moderators can change the highlighting language for a tag.)

How do I report a bug or request a new language?

If it is indeed a bug in the syntax highlighter itself, check for existing reports first to see if it has already been reported; if it hasn't, go ahead and report it there. For bug fixes and features that have already been implemented by highlight.js but aren't working here, please wait, as new versions of highlight.js are deployed on the sites on a scheduled basis, and the latest version hasn't been deployed yet. (If it hasn't been deployed after a long time, you can raise a feature request on Meta to request that a new version of highlight.js be deployed.)

Keep in mind that Stack Exchange does not maintain this syntax highlighter (aside from installing newer versions of it), and posting bug reports or feature requests concerning it here on Meta will not get them fixed or implemented.

Before you do anything, make sure that you've got the correct highlighting turned on.

How do I use syntax highlighting?

As long as all the conditions in the Why isn't my code being highlighted correctly? section above are satisfied, highlighting will kick in automatically on code blocks based on the tags on the question. (See 4. Check that the tags on your question have syntax highlighting enabled above for how this works.) Inline code (e.g. this) is not highlighted.

It is possible to explicitly override the default highlighting language in use on the post with your language of choice in a specific code block, by specifying a language hint above the code block. Note that this is only supported when using the code fence (```) method of code formatting; as of the implementation of CommonMark, doing so on code blocks using the four-space indent method is no longer supported*:

code goes here

You may use either a language code or a tag name in the language hint to activate syntax highlighting. See below for the complete list of highlight.js-supported language codes.

For example:

Here is a code block with language code (with "lang-" prefix) as a hint:

function greet(person) {
    return "Hello " + person;
var user = "John Doe";

Here is a code block with tag name (no "lang-" prefix) as a hint:

var arr = [0, 1, 2];  // will highlight if typescript tag exists

If you don't want to have any syntax highlighting, you can use the lang-none language:

[code here]

You can also apply a language hint to all code blocks in your post, so you don't have to add a hint before each one, by adding an HTML comment at the top of your post. The hint will then be applied to all code blocks within your post, including those which use four-space indent or HTML <pre><code>:

<!-- language-all: lang-or-tag-here -->

Note that when used on a question, it does not override the highlighting language on its answers; those will still be highlighted by default as per the question's tags.

Language codes currently available on Stack Exchange

This is a complete list of every identifier that you can use in the language hint for syntax highlighting. All those language hints in each group point to the same highlighter. Other language aliases set by highlight.js might work, but only the following identifiers are officially supported by Stack Exchange.

  • Default: interpret the code and guess lang-default
  • None: explicitly do not use any syntax highlighting lang-none, lang-plaintext, lang-text, lang-txt
  • Bash lang-bash, lang-sh, lang-zsh
  • C, C++, and other C-likes lang-c, lang-h, lang-cpp, lang-hpp, lang-c++, lang-h++, lang-cc, lang-hh, lang-cxx, lang-hxx, lang-c-like
  • C# lang-cs, lang-csharp, lang-c#
  • Clojure lang-clojure, lang-clj
  • CoffeeScript lang-coffee, lang-coffeescript, lang-cson, lang-iced
  • CSS lang-css
  • Dart lang-dart
  • Erlang lang-erl, lang-erlang
  • Go lang-go, lang-golang
  • Haskell lang-hs, lang-haskell
  • HTML, XML, XSL, et al. lang-html, lang-xml, lang-xsl, lang-xhtml, lang-rss, lang-atom, lang-xjb, lang-xsd, lang-plist, lang-wsf, lang-svg
  • HTTP Headers lang-http, lang-https
  • Ini and TOML lang-ini, lang-toml
  • Java lang-java, lang-jsp
  • JavaScript lang-js, lang-javascript, lang-jsx, lang-mjs, lang-cjs
  • JSON lang-json
  • Kotlin lang-kotlin, lang-kt
  • LaTeX, TeX lang-latex, lang-tex
  • Less lang-less
  • Lisp lang-lisp
  • Lua lang-lua
  • Makefile lang-makefile, lang-mk, lang-mak
  • Markdown lang-markdown, lang-md, lang-mkdown, lang-mkd
  • MATLAB lang-matlab
  • Objective-C lang-objectivec, lang-mm, lang-objc, lang-obj-c
  • OCaml lang-ocaml, lang-ml
  • Pascal, Delphi lang-pascal, lang-delphi, lang-dpr, lang-dfm, lang-pas, lang-freepascal, lang-lazarus, lang-lpr, lang-lfm
  • Perl lang-pl, lang-perl, lang-pm
  • PHP lang-php, lang-php3, lang-php4, lang-php5, lang-php6, lang-php7
  • PHP Template lang-php-template
  • Protocol Buffers lang-protobuf
  • Python lang-py, lang-python, lang-gyp, lang-ipython
  • R lang-r
  • Ruby lang-rb, lang-ruby, lang-gemspec, lang-podspec, lang-thor, lang-irb
  • Rust lang-rs, lang-rust
  • Scala lang-scala
  • Scheme lang-scheme
  • SCSS lang-scss
  • Shell lang-shell, lang-console
  • SQL lang-sql
  • Swift lang-swift
  • TypeScript lang-typescript, lang-ts
  • VHDL lang-vhdl
  • VB.NET lang-vbnet, lang-vb
  • YAML lang-yaml, lang-yml

Any language identifiers used in a post that go unrecognized by highlight.js will functionally default to lang-default.

Hinting: Tags

You can specify any tag that exists on the site, and it will use whatever language code is currently associated with that tag (which can be either null (no hint), default, or a specific language-code).

Keep in mind that by default all tags start off with none as their language code. Tags with none specified as their language code will be ignored and revert to lang-default.

You can also use the plain none keyword to manually specify no syntax highlighting, similar to using the lang-none code above.

* The former method of specifying a highlighting language can still be used for HTML code blocks: place an HTML comment <!-- language: lang-or-tag-here --> before the <pre><code> tags and it will work.

Also, this former method hasn't been completely removed for four-space indented code blocks, but merely deprecated. While it will still work for the time being on four-space indented code blocks, it may/will be removed in the future.

Note to editors:

Please do not add to the above list unless you are 100% sure it exists.

  • Just because you type something in and it looks like it's highlighted correctly does not mean the identifier actually exists in the system. Keep in mind that invalid identifiers revert back to default.

  • Additionally, several languages that are indicated as supported in highlight.js's documentation may not actually be supported by the Stack Exchange version of highlight.js, as they may be in separate modules which may not be deployed on Stack Exchange.

Because of these points, please link to the Meta question which confirms a hint's existence in your edit summary when adding a new hint to the list.

Note to commenters:

The comments on this FAQ are for requesting clarification of something you might not understand in the FAQ so that it can be fixed. Please DO NOT ask if certain languages will be supported in the future. That is not a question we can answer because Stack Exchange does not maintain this highlighter. Visit highlight.js for language support.

  • 2
    Shouldn't there be an indicated preference (not rule) here in this answer for using either a language code or a tag name (possibly with some qualifiers)? E.g. "Prefer to use X over Y, unless Z." (Somewhat related question on MSO (in comments): What's the difference between the two code formatting styles (code-fences vs indented blocks) regarding syntax highlighting?) – P.Mort. - forgot Clay Shirky_q Oct 16 '20 at 16:00
  • and it will use whatever language code is currently associated with that tag Is there an official list of these mappings available anywhere? – Josh Goebel Oct 29 '20 at 1:05
  • Section 4, "4. Check that the tags on the question have syntax highlighting enabled", has all the info to find out @JoshGoebel – ben is uǝq backwards Oct 29 '20 at 5:41
  • @Josh To your direct question, I don't believe there is a list anywhere currently containing all tags with associated language, however. It also doesn't look like this info is accessible using SEDE either, it's not in the tags table. – zcoop98 Oct 29 '20 at 14:32
  • Is there no lang-* tag for PowerShell? – Thomas Dec 17 '20 at 7:38
  • 1
    Powershell currently uses the default @Thomas (see bottom of the page). If there's nothing on the list then there isn't one. – ben is uǝq backwards Dec 17 '20 at 11:00
  • Lack of lang-xaml as alias for lang-xml. Not in this post but on StackOverflow. – aepot Jan 21 at 22:03
  • @aepot Where did xaml come from? I'm not seeing it here in this list, or in the official highlight.js list either. Can you elaborate on what you mean? – zcoop98 Jan 21 at 22:14
  • This post is the result of a lot of analysis and the work of a number of devs @aepot. Can you link to anything that verifies what you're saying? – ben is uǝq backwards Jan 21 at 22:14
  • @benisuǝqbackwards the post is fine. I'm about adding that to SO highlighter because i often meet the mishighlighted code in the posts tagged with xaml on StackOverflow, and lang-xaml isn't supported. (sorry for my bad English). – aepot Jan 21 at 22:27
  • @aepot Okay, I think I gotcha. You're saying that "xaml" code should be highlighted by the "xml" highlighter, and that there should be a new alias of lang-xaml --> lang-xml. We don't necessarily need a new alias here, rather, the best course of action would probably be to propose that the default highlighter for the [xaml] tag be set to lang-xml. – zcoop98 Jan 21 at 22:41

You must log in to answer this question.

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