vimvim-syntax-highlighting

How do I change vim's syntax highlighting for multiline comment in *.py files?


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 '''...


Solution

  • 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:

    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.