1

How do I change syntax color of multiline comment in python source codes? Line

hi def link pythonComment       Comment

in /usr/share/vim/vim82/syntax/python.vim seems to cover only singleline comments. Meanwhile changing comments in C/C++ seems to work on both single and multilines. But python is more difficult. There are few places mentioning """ or '''...

1
  • 2
    """ and ''' are not multiline comments, they're multiline strings. Commented Aug 22, 2024 at 23:17

1 Answer 1

3

As mentioned in the comments, """ and ''' don't mark multiline comments. Depending on the context, they can be called multiline strings, as in:

foo = """bar
baz"""

or documentation strings ("docstrings" for short), as in:

"""foo bar baz"""

which are not "comments" in the common meaning of the word.

See…

I don't have Vim 8.2 handy (current version is 9.1, consider upgrading) but, in my version, the correct highlight groups for multiline strings are:

  • PythonString for the whole string, including the opening and closing """ and ''',
  • also PythonTripleQuotes for the opening and closing """ and ''',

both of which being ultimately linked to String for obvious reasons.

If you really want to highlight docstrings differently from multiline strings, you will have to create your own highlight group because the built-in syntax script doesn't treat them differently. They are all "strings".

But I think you should leave it as-is in order to preserve the semantics of it all.

Sign up to request clarification or add additional context in comments.

1 Comment

A documentation string is a particular use of a string literal, regardless of the quotation style employed. The particular style that involves """ or ''' is referred to as a triple-quoted string. ("""...""" and '''...''' don't have individual names.) The section of the language documentation on string and byte literals mention nonterminals named shortstring and longstring, but those aren't commonly used terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.