Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

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

3 Answers 3

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

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.

share|improve this answer

Your Answer

 
discard

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.