1

I just discovered the crazy awesome Vim plugin OnSyntaxChange. My first usage attempt is to enable text wrapping when editing Python comments and multi-line strings.

The first part was easy, as it's basically one of the documented examples:

autocmd Filetype python call OnSyntaxChange#Install('Comment', '^Comment$', 1, 'i')
autocmd Filetype python autocmd User SyntaxCommentEnterI setlocal textwidth=80
autocmd Filetype python autocmd User SyntaxCommentLeaveI setlocal textwidth=0

However, it doesn't seem like the built-in Python syntax file makes any distinction between multi-line and single-line Python comments.

I used SyntaxAttr.vim (props to Ingo, he made both these plugins) to check the syntax group of strings in Python, and they all show:

group: pythonString->String

How do I solve this? Is there an alternative syntax file I can use? Or could I solve this with a minor modification to the built-in one?

1 Answer 1

0

Looks like it's necessary to take a look at the source code. (so this might break later in future Vim versions because it depends on specific group-name values in the original syntax file.)

From $VIMRUNTIME/syntax/python.vim:

" Triple-quoted strings can contain doctests.
syn region  pythonString matchgroup=pythonQuotes
      \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
      \ contains=pythonEscape,@Spell
syn region  pythonString matchgroup=pythonTripleQuotes
      \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
      \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
syn region  pythonRawString matchgroup=pythonQuotes
      \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
      \ contains=@Spell
syn region  pythonRawString matchgroup=pythonTripleQuotes
      \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
      \ contains=pythonSpaceError,pythonDoctest,@Spell


hi def link pythonString        String
hi def link pythonRawString     String

So we can add

syn region  pythonTripleQuotedString matchgroup=pythonTripleQuotes
      \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
      \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
syn region  pythonRawTripleQuotedString matchgroup=pythonTripleQuotes
      \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
      \ contains=pythonSpaceError,pythonDoctest,@Spell
hi def link pythonTripleQuotedString        String
hi def link pythonRawTripleQuotedString     String

to .vim/after/syntax/python.vim.

Looks like everything works as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.