67

It seems as though often the syntax highlighter does not detect the language I'm working in.

When this happens, I try to be responsible and apply the manual highlighting comment: <!-- language: NAME -->.

However, it's hard for me to remember this syntax because it is finicky.

Consider the following examples of the highlighting comment with subtle changes in spacing:

<!-- language: python -->

for i in range(10):
  print('hi')

<!--language: python -->

for i in range(10):
  print('hi')

<!-- language: python-->

for i in range(10):
  print('hi')

<!--language:python-->

for i in range(10):
  print('hi')

<!-- language:python -->

for i in range(10):
  print('hi')

<!-- language: python -->

for i in range(10):
  print('hi')

Since the foregoing doesn't display on Meta, I have an image:

Of the six examples above, only the first results in correct highlighting. All others display as plain pre-formatted text.

In all cases, the comments are detected as such, but in only some cases is highlighting applied as a result.

Is there a way to make this command less finicky and, hopefully, thereby easier to use?

17
  • 50
    Does this only apply to python or also other languages? If its only python maybe we can just get rid of that language :P Commented Apr 16, 2018 at 21:47
  • 22
    Any question about Python should be tagged [python]. That takes care of selecting the proper highlighting. Commented Apr 16, 2018 at 21:50
  • 20
    @HansPassant Unless it's also tagged with another, more popular, language tag, say because the question involved code from multiple languages. Commented Apr 16, 2018 at 21:51
  • 14
    @Servy, Surely no language is more popular than Python! Commented Apr 17, 2018 at 1:45
  • 6
    @Servy: Oddly enough, the other day I had to add lang-js hints to JS blocks on a question that was tagged [javascript] and [python], because they were being treated as Python code. Commented Apr 17, 2018 at 2:27
  • 8
    You would actually be able to display the syntax highlighting on meta without a screenshot if you used the Prettify language identifiers instead of the tag names (e.g. lang-py instead of python). The colors are much more subtle though. Commented Apr 17, 2018 at 2:29
  • @AndréKool: I assume this applies to other languages, though I have not tested it. Commented Apr 17, 2018 at 3:26
  • 4
    @HansPassant: Multiple languages could be present in a question or answer, so there's ambiguity. Regardless, I this, while helpful, dodges the central issue: the current setup makes it (needlessly?) difficult to specify highlighting. Commented Apr 17, 2018 at 3:27
  • 8
    is there somewhere a list of all supported syntax highlighted languages/codes. We could then make a bookmarklet that would add a simple select list that copies the right one in. Commented Apr 17, 2018 at 8:31
  • 4
    You’ve proven that there are a lot of white-space permutations possible, but, seriously, is <!-- single-space language: single-space name single-space --> really so hard to remember? There is no space before the colon, but that’s the standard typesetting on computers anyway. Commented Apr 17, 2018 at 11:34
  • 12
    IMO SO should support the fenced code block style (i.e. ```python\ncode goes here\n```) Commented Apr 17, 2018 at 11:52
  • 2
    @Holger The only thing I sometimes forget is if it's <!-- language: ... --> or <!-- lang: ... -->. The reason why I sometimes make this mistake is because you should actually use lang-py in stead of python, so sometimes I accidentally write <!-- lang: lang-py -->. Bookmarking What is syntax highlighting and how does it work? helped a lot. Commented Apr 17, 2018 at 11:57
  • 1
    @JF Cf. Support CommonMark fenced code blocks and Will CommonMark be adopted for SE? (Meta.SE). Commented Apr 17, 2018 at 13:28
  • 2
    When I need to highlight, I google "syntax highlight stackoverflow" which gets me here from which I inferred language: python. language: lang-py is not something I'd've guessed, especially with language: c# as an example. Commented Apr 17, 2018 at 19:07
  • 1
    I'd be for making a separate button with a keyboard shortcut that would open a popup dialog where you could select syntax highligting for the selected block. Languages used in the tags could have been listed on top of the list for example. Commented Apr 18, 2018 at 16:32

1 Answer 1

5

If you use code fences, this is easy to remember.

Simply create the code fence and put the language right next to it:

```python
for i in range(10):
  print('hi')
```

Result:

for i in range(10):
  print('hi')

Of course, it's not as easy as it could be (you can make a mistake typing the language), but it's a lot better than what we were limited to before code fences.

1
  • Worth pointing out that ```python tells the highlighter to look at the [python] tag for a language hint, and does not specify the language directly. For that, use ```lang-python. This usually doesn't matter, since the language tags are in general well-kept, but it does matter for some, eg. [typescript] (uses lang-js even though lang-ts exists). Commented Mar 1, 2021 at 19:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.