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

1 Answer 1

up vote 140 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.

You can also submit a request for a new language to be added in that same issues list. Keep in mind that Stack Exchange does not maintain this syntax highlighter, and posting bug reports or feature requests concerning it here on Meta will not get them fixed or implemented.

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. If you're curious whether a tag has a language hint, any user is capable of checking by visiting that tag's wiki page. 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

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:

  • Let Prettify interpret the code and guess.
    default
  • Explicitly do not use any syntax highlighting.
    lang-none
  • Bash and other Shell scripting
    lang-bash, lang-bsh, lang-csh, lang-sh
  • C, C++, et al
    lang-c, lang-cc, lang-cpp, lang-cxx, lang-cyc, lang-m
  • C#
    lang-cs
  • Clojure
    lang-clj
  • CoffeeScript
    lang-coffee
  • CSS
    lang-css
  • Go
    lang-go
  • Haskell
    lang-hs
  • HTML
    lang-html
  • Java
    lang-java
  • JavaScript
    lang-js, lang-javascript
  • JSON
    lang-json
  • LaTeX and TeX
    lang-latex, lang-tex
  • Lisp and Scheme
    lang-cl, lang-el, lang-lisp, lang-lsp, lang-scm, lang-ss, lang-rkt
  • Lua
    lang-lua
  • OCaml, SML, F#, et al
    lang-fs, lang-ml
  • Pascal
    lang-pascal
  • Perl
    lang-pl, lang-perl
  • PHP
    lang-php
  • Protocol buffers
    lang-proto
  • Python
    lang-py, lang-python, lang-cv
  • R and S
    lang-r, lang-s
  • Regex
    lang-regex
  • Ruby
    lang-rb, lang-ruby
  • Rust
    lang-rc, lang-rs, lang-rust
  • Scala
    lang-scala
  • SQL
    lang-sql
  • VHDL
    lang-vhdl, lang-vhd
  • Visual Basic
    lang-vb, lang-vbs
  • XML
    lang-xml

Tags:

You can specify any tag that exists on the site, and it will use whatever language code is currently specified for that tag. 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 default.

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

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 Google Code Prettify for language support.

share|improve this answer
    
Thank you @animuson; that's been on my to do list for a while. It raises some questions that I can't find documented though; is sh the same as bsh and cv the same as py and python? –  ben is uǝq backwards Jul 25 at 17:53
    
Yes. They are grouped together based on how they appear in the Prettify JS file that Stack Exchange loads. All those language hints in each group point to the same highlighter. They are synonymous in every way. –  animuson Jul 25 at 17:54
    
There is something fishy about the "Note to commenters". I've followed the link to the Prettify project, it hasn't seen a release within more than a year. The youngest "accepted" issue is from August, 2012, it looks pretty dormant. Many languages that SO supports are missing from its code base, like PHP, C# or CoffeScript. So obviously SO is adding languages on their own, and they are not fed back into the project. It looks more promising to bug you guys :-). Suggestion: Make a fork of the project on Github, add your language files, accept pull requests, and get on with it. –  ThomasH Jul 26 at 21:48
    
@ThomasH Some of the primary ones are integrated into Prettify and can't actually be removed. You can see those in the main prettify.js file. It may be fairly inactive, but it's certainly not abandoned. The most recent change was made March 3, 2014. –  animuson Jul 27 at 3:57
    
How do we request a change to the language specified for a tag? For example, the xpath tag has default for its highlighting language. This leads to unfortunate events like lines beginning with //foo getting colored pale like a comment. In the absence of highlighting support for XPath itself, lang-xml would be better than the default. I tried editing the tag wiki for xpath, but the language setting didn't seem to be part of what was editable (maybe based on my rep). Do I need to raise a feature request on meta? –  LarsH Aug 28 at 16:28
    
Ask on meta.stackoverflow.com or this meta @Lars. –  ben is uǝq backwards Aug 28 at 16:54
3  
I think the help page should link this post rather than this one: that one doesn't have the actual supported list. –  bfred.it Sep 13 at 12:47
    
Is it safe to assume that lang-X and X are equivalent if X tag exists? –  J.F. Sebastian Oct 3 at 12:11
    
It's probably safe but it's not guaranteed @J.F.Sebastian; the languages are manually applied to the tags by SE/mods so there's always a chance that they could be different. –  ben is uǝq backwards Oct 3 at 12:59
1  
lang-regex just highlights everything in a single color, it doesn't appear to be supported by the Prettify library. –  Lucas Trzesniewski Oct 29 at 19:40
1  
I support @bfred.it 's suggestion! I had to search high and low everywhere just to find this helpful answer. –  pepoluan Oct 30 at 3:36
    
Third the motion :) I had to search just as much as @pepoluan –  Timtech Nov 18 at 19:40

You must log in to answer this question.

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