Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight python docstrings as comments (vim syntax highlighting)

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
like image 778
silvernightstar Avatar asked Apr 16 '13 15:04

silvernightstar


2 Answers

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.

like image 60
Akobold Avatar answered Sep 19 '22 13:09

Akobold


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.

like image 37
silvernightstar Avatar answered Sep 19 '22 13:09

silvernightstar