I have some C++ source files in a GitHub repository 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?

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.

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!

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.

Sharing my answer here I discovered elsewhere on SO.

I discovered that you can add a vim or emacs modeline per the Linguist readme to the top of your source file (unfortunately, required for each file) to coerce syntax highlighting for Github.com. Based on the languages.yml file, I think you'd need to add /* vim: syntax=C++ */ to your source file.

You can now force the language of any file in your repositories using Linguist overrides. Linguist is the open source library detecting the language of files across github.com.

To force your .c files to be highlighted using the C++ grammar, you can add the following in your .gitattributes file:

*.c linguist-language=C++
  • I've tried this for *.S linguist-language=asm to no avail.... – mckenzm Jul 18 at 11:33
  • @mckenzm It seems your message got truncated. The .gitattributes line you posted should force Linguist to recognize all .S files as Assembly. If it doesn't work, I can have a look at the repository to try to see what's happening (do you have a link?). – pchaigno Jul 18 at 16:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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