5

I use python and in python, three quotation marks in a row

"""

or

'''

signify a block-comment, and a normal comment is just a hashtag. (#) I want comments to be colored grey and strings to be colored orange. I am using the colorscheme murphy. I made a murphy-mine.vim file and I added this to it

hi Comment term=bold     ctermfg=Grey     guifg=Grey
hi String  term=bold     ctermfg=LightRed guifg=Orange

But gvim sees """ and ''' as a string in python syntax rather than treating it as a comment (block comment). Is there a way for me to tell gvim to see """ and ''' as a comment / block comment rather than a string?

2
  • 2
    Docstrings are strings so it's natural that Vim treats them as such. Those are for documentation, not for commenting out entire blocks. If you want to comment some code, use # comments.
    – romainl
    Commented Jan 5, 2014 at 21:35
  • see also stackoverflow.com/a/16043095/127971
    – michael
    Commented Jun 27, 2020 at 1:46

2 Answers 2

5

You can put the following into ~/.vim/after/syntax/python.vim (taken from here:

" Highlight docstrings as comments, not string.
syn region pythonDocstring  start=+^\s*[uU]\?[rR]\?"""+ end=+"""+ keepend excludenl contains=pythonEscape,@Spell,pythonDoctest,pythonDocTest2,pythonSpaceError
syn region pythonDocstring  start=+^\s*[uU]\?[rR]\?'''+ end=+'''+ keepend excludenl contains=pythonEscape,@Spell,pythonDoctest,pythonDocTest2,pythonSpaceError

hi def link pythonDocstring pythonComment
2
  • Why are the two syn region lines repeated? They look identical to me..
    – lorenzog
    Commented Nov 5, 2015 at 15:59
  • @lorenzog: One is for single-, the other for double-quoted strings. Subtle, right :-) Commented Nov 5, 2015 at 16:37
0

PEP 257 prescribes to use """triple double quotes""" for docstrings. It's not obligatory to include '''triple single quotes''' or "single double quotes" into docstrings. There is one difficulty that we have class docstrings, function docstrings, module docstrings, attribute docstrings and additional docstrings. That's why I decided that it's easier to consider docstring as following:

syn region pythonDocString start=+^\s*"""+ end=+"""+ keepend contains=...

And then:

HiLink pythonDocString        Comment

You may see examples in this script (search pythonDocString): https://github.com/andbar-ru/python-syntax/blob/master/syntax/python.vim

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .