Web Applications Stack Exchange is a question and answer site for power users of web applications. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some C++ source files in a GitHub repo which have an extension of .C and are using C syntax highlighting, which I don't want.

How can I set GitHub to apply C++ syntax highlighting to these files?

Is there a method to how GitHub identifies the source language for syntax highlighting purposes? It looks like it uses only the file extension, but is there a way to force a particular language?

share|improve this question

I asked support@github.com; this is their reply:

We use Pygments (http://pygments.org/) to do syntax highlighting and determine which lexer to use based on each file's extension. So unfortunately, it doesn't look like there's a way to get C++ highlighting without renaming the files from *.c -> *.cpp and *.h -> *.hpp.

You can do this without losing git history of a file by using the git mv command.

share|improve this answer

GitHub's language detection is done by the Linguist module, which is conveniently open source. It relies primarily on the file extension to detect the language, although it can be a bit clever to detect ambiguous files (such as .h files). As you can see from the configuration file, .c is firmly defined as a C file. Given the number of files that have to be assessed on GitHub, efficiency is a key requirement, if at the cost of some accuracy.

It looks like per repo configurations have been ruled out by the developer, so the only way you could make files syntax highlight as C++ would be to use C++ extensions. To keep those extensions and have highlighting you may have to try another service, or host the code somewhere yourself.

Update Oct 2014: GitHub haven't stood still, in the last year they have introduced some basic heuristics to help determine the language in the file. More specific to this questions, .c files are now checked to see if they are C, C++ or Objective-C.

GitHub support's answer is a little interesting, they do use Pygments for highlighting and the lexer, but the rulesets are in their own Linguist module. Which you'd hope they'd know!

share|improve this answer

For files with a Shebang, the Shebang is considered when determining the language but seems to be evenly weighted against other tokens. This seems to be a big error because the Shebang should definitively define the language of the file. This can cause issues with highlighting.

As a workaround you can add dummy tokens in the form of a comment to "tip the scales" in favor of the correct language. This is experimental but I have had luck with it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.