突出显示python docstrings作为注释(vim语法高亮显示)

sil*_*tar 16 vim python-2.7 vim-syntax-highlighting

是否可以修改python.vim(和相应的colorscheme文件),以便在vim下python语法高亮显示时,在class和def语句(aka docstrings)下的三引号字符串将被突出显示为注释?

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
Run Code Online (Sandbox Code Playgroud)

Ako*_*old 18

您可以添加以下行:

syn region Comment start=/"""/ end=/"""/
Run Code Online (Sandbox Code Playgroud)

到你的〜/ .vim/after/syntax/python.vim.如果该文件不存在,您可以创建该文件.

  • 谢谢,这确实有效,但它也会导致字符串赋值,如string_var ="""blah blah"""也被强调为注释.现在意识到python docstring"start"的定义特征是一个三重引号,在行的开头没有非空白字符.你的回答确实引导我,谢谢. (4认同)

sil*_*tar 6

以下对我有用:

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
Run Code Online (Sandbox Code Playgroud)

这里修改python.vim .