What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 122 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 88 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: javascript -->

    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-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
  • rb
  • sh
  • xhtml
  • xml
  • xsl
share|improve this answer
    
I've added several lang-s derived from the source code. Some syntax rules have several aliases, so I've grouped them together. If you think that it's better to have one lang per line, just edit the post. –  Rob W Oct 10 '13 at 15:00
    
@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
    
But the indentation doesn't seem to be respected, or performed. –  GGB667 Nov 6 '13 at 15:14
    
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
    
It's linked above @Chris in the section "How do I report a bug or request a new language?". –  ben is uǝq backwards Dec 17 '13 at 12:27
    
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
    
@David The JS not keyword is !. –  kitcar2000 Mar 22 at 22:16
    
@benisuǝqbackwards now, just for completeness, is default and lang-default the same? –  Mark Hurd Mar 27 at 9:45
    
I couldn't find it in the code @Mark so I'm not sure... –  ben is uǝq backwards Mar 27 at 10:04
    
I wish there was one for xpath. For now I'm specifying it as lang-bash, as it renders the most readably. –  TJamesBoone Mar 29 at 20:25
4  
Why does the example use <!-- language: javascript --> when javascript isn't in the list of acceptable language tags? –  drs Mar 31 at 14:42
    
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 2 days ago
add comment

You must log in to answer this question.

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