17

Is it possible to modify python.vim (and the corresponding colorscheme file) such that triple-quoted strings right under class and def statements (a.k.a. docstrings) will be highlighted as comments during python syntax highlighting under vim?

class URLopener:
  """Class to open URLs.
  This is a class rather than just a subroutine because we may need
  more than one set of global protocol-specific options.
  Note -- this is a base class for those who don't want the
  automatic handling of errors type 302 (relocated) and 401
  (authorization needed)."""

def addheader(self, *args):
  """Add a header to be used by the HTTP interface only
  e.g. u.addheader('Accept', 'sound/basic')"""

# sample comment
silvernightstar
  • 1,885
  • 3
  • 19
  • 25
  • @EricLeschinski I have python.vim 3.3.6, and it doesn't for me. Docstrings are highlighted as string constants, not comments. – marcelm Sep 11 '21 at 16:30

3 Answers3

18

you can add the following line:

syn region Comment start=/"""/ end=/"""/

to your ~/.vim/after/syntax/python.vim. You can create this file if it doesn't exists.

Akobold
  • 936
  • 8
  • 25
  • 4
    Thanks, that does work, but it will also cause string assignments such as string_var = """blah blah""" to be highlighted as comments as well. Realized now that the defining characteristic of a python docstring "start" is a triple-quote not preceded by non-whitespace characters in the beginning of a line. Your answer did lead me to it, thanks. – silvernightstar Apr 17 '13 at 03:37
  • It works for me,thanks, can you explain to me thy this file will be automatelt included in vim run environment? – Statham Aug 01 '19 at 05:45
9

The following worked for me:

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

Taken from a modified python.vim from here.

silvernightstar
  • 1,885
  • 3
  • 19
  • 25
  • The link to the syntax file in the post is now broken, so thanks for capturing that. For completeness' sake, you may also want to include `hi link pythongDocstring Comment` in the code sample. – ches Jul 02 '16 at 14:20
  • Here is a link to this file on the WebArchive: https://web.archive.org/web/20160503053615/http://ianbits.googlecode.com/svn/trunk/vim/python.vim – Gleb Sabirzyanov Apr 27 '18 at 21:46
  • This answer could use an update, as python syntax for vim is more complex/involved – Dima Tisnek Dec 17 '18 at 06:17
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

Andrey
  • 1,495
  • 17
  • 14