What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 127 Stack Exchange communities.

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

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

Return to FAQ index

share|improve this question
add comment

1 Answer

up vote 108 down vote accepted

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 Google Code Prettify. Therefore, any bugs and feature requests regarding syntax highlighting cannot be handled by Stack Exchange and should be directed to the team behind Google Code Prettify.

Syntax highlighting is assigned 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?

If your post doesn't have the correct highlighting, it's possible it's not supported. Please look at the list of supported languages. If your language is not on the list, it needs to be created within the Prettify project before it can be deployed by Stack Exchange. If a language that could be applied to a tag is already on the list, please raise a feature request here on Meta to have it deployed on the network.

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

If it is indeed a bug in the syntax highlighter itself, check the issues list to see if it has already been reported. If it hasn't, feel free to report it or join the project and submit a fix yourself. If you want to ensure that an issue you raised is fixed quickly, it's best to include the fix in the report. If the fix has already been implemented by Prettify but is still not working here, please raise a feature request on Meta to request that a new version of Prettify be deployed.

Before you do anything, are you sure that you've got the correct highlighting turned on? Behind the scenes, Stack Exchanges uses the tags on the question to infer the language you are using. If there's more than one tag that has syntax highlighting, it uses a default and lets Prettify infer what's the best language to use.

Try to explicitly override the highlighting in use with your language of choice:

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

    code goes here

For instance,

<!-- language: lang-js -->

    var a = 3;
    while( !(a < 0)){
        alert("JavaScript code <b>goes</b> here.");
        a--;
    }

renders as:

var a = 3;
while( !(a < 0)){
    alert("JavaScript code <b>goes</b> here.");
    a--;
}

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

<!-- language: lang-none -->

If the selected language applies to all code blocks in your post, you can also use the <!-- language-all: lang-or-tag-here --> highlighting hint to have it be applied to all code blocks (so you don't have to add a hint before each one).

Available Syntax Highlighting Identifiers

This is a complete list of every identifier that you can use in the language hint for syntax highlighting. You may use either a language code or a tag in the language hint to activate syntax highlighting.

Please do not add to this 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. Please link to the Meta question which confirms a hint's existence in your edit summary when adding a new hint to the list.

Language Codes:

  • default
  • lang-none
  • lang-bash, lang-bsh, lang-csh, lang-sh
  • lang-c, lang-cc, lang-cpp, lang-cxx, lang-cyc, lang-m
  • lang-cl, lang-el, lang-lisp, lang-lsp, lang-scm, lang-ss, lang-rkt
  • lang-clj
  • lang-coffee
  • lang-cs
  • lang-css
  • lang-fs, lang-ml
  • lang-go
  • lang-hs
  • lang-html
  • lang-java
  • lang-js, lang-javascript
  • lang-json
  • lang-latex, lang-tex
  • lang-lua
  • lang-pascal
  • lang-php
  • lang-pl, lang-perl
  • lang-proto
  • lang-py, lang-python, lang-cv
  • lang-r, lang-s
  • lang-rb, lang-ruby
  • lang-rc, lang-rs, lang-rust
  • lang-regex
  • lang-scala
  • lang-sql
  • lang-vb, lang-vbs
  • lang-vhdl, lang-vhd
  • lang-xml

Tags:

  • none
  • bsh
  • c
  • cc
  • cpp
  • cs
  • csh
  • cyc
  • cv
  • htm
  • html
  • java
  • js
  • m
  • mxml
  • perl
  • pl
  • pm
  • py
  • r
  • rb
  • sh
  • xhtml
  • xml
  • xsl
share|improve this answer
2  
@RobW I like them grouped together like that since they go to the same thing. I didn't think about looking at the source. I had just copied the list that moderators see in the drop-down menu. –  animuson Oct 23 '13 at 6:59
1  
But the indentation doesn't seem to be respected, or performed. –  GGB667 Nov 6 '13 at 15:14
1  
lang-ml (assuming that this is the proper language code for functional languages like Standard ML) does not seem to behave correctly: // is treated as a line comment, but as far as I know neither Standard ML nor e.g. OCaml have those (only block comments with (* and *)). Where is the proper place to complain about this? ;) –  chris Dec 17 '13 at 9:47
1  
Is google-code-prettify dead? This Ada patch has had no developer response for months: code.google.com/p/google-code-prettify/issues/detail?id=312 –  CyberShadow Jan 15 at 21:39
1  
Great Q&A. BTW I don't think JS has a not keyword... –  David Feb 27 at 2:59
5  
Why does the example use <!-- language: javascript --> when javascript isn't in the list of acceptable language tags? –  drs Mar 31 at 14:42
2  
Though those languages may exist in the Prettify code base they're not included in Stack Overflow's version @Mike. SE doesn't use all of the possibilities as it means more bytes. If you'd like to include them you could raise a feature request here or on MSO. –  ben is uǝq backwards Apr 21 at 17:31
1  
Beta sites don't support syntax highlighting? –  Dan Apr 29 at 7:34
1  
Are you sure those tags are correct? I've seen javascript, python, ruby being used (yielding correct results), not js, py, rb. –  nyuszika7h May 3 at 14:15
1  
What about Yaml? –  Slava Fomin II May 6 at 11:48
1  
Fortran? –  Kyle Kanos May 22 at 18:25
1  
I just tried "html" and it didn't work. "lang-html" did work. –  John Bachir Jun 1 at 21:56
1  
I think that you missed lang-d in your list. –  kitcar2000 Jun 3 at 16:34
1  
Is there any chance to get line numbering enabled? –  Kerrek SB Jun 13 at 1:31
1  
It's been ignored for 5 years @Kerrek... so, probably not. –  ben is uǝq backwards Jun 15 at 19:29
show 8 more comments

You must log in to answer this question.

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