Skip to content

Instantly share code, notes, and snippets.

@phelipetls
Last active January 19, 2024 18:27
Show Gist options
  • Save phelipetls/8726d6cd68e66ad6b83586ae53f3b3d8 to your computer and use it in GitHub Desktop.
Save phelipetls/8726d6cd68e66ad6b83586ae53f3b3d8 to your computer and use it in GitHub Desktop.
" ~/.vim/after/syntax/python.vim or ~/.config/nvim/after/syntax/python.vim
syn match pythonEscape +{{+ contained containedin=pythonfString,pythonfDocstring
syn match pythonEscape +}}+ contained containedin=pythonfString,pythonfDocstring
syn region pythonfString matchgroup=pythonQuotes
\ start=+[fF]\@1<=\z(['"]\)+ end="\z1"
\ contains=@Spell,pythonEscape,pythonInterpolation
syn region pythonfDocstring matchgroup=pythonQuotes
\ start=+[fF]\@1<=\z('''\|"""\)+ end="\z1" keepend
\ contains=@Spell,pythonEscape,pythonSpaceError,pythonInterpolation,pythonDoctest
syn region pythonInterpolation contained
\ matchgroup=SpecialChar
\ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
\ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue,pythonDoctest
syn match pythonStringModifier /:\(.[<^=>]\)\?[-+ ]\?#\?0\?[0-9]*[_,]\?\(\.[0-9]*\)\?[bcdeEfFgGnosxX%]\?/ contained containedin=pythonInterpolation
syn match pythonStringModifier /![sra]/ contained containedin=pythonInterpolation
syn match pythonStringModifier /\zs *= *\ze[}:!]/ contained containedin=pythonInterpolation
hi link pythonfString String
hi link pythonfDocstring String
hi link pythonStringModifier PreProc
@phelipetls
Copy link
Author

That would make sense, this files assumes you're using the default highlight groups, bundled with Vim.

All groups will be highlighted, except things that aren't expressions (should probably contain more stuff here but I forgot):

syn region pythonInterpolation contained
      \ matchgroup=SpecialChar
      \ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
      \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue,pythonDoctest

After a quick look, it seems this would make sense if you're using python-syntax:

syn region pythonInterpolation contained
      \ matchgroup=SpecialChar
      \ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
      \ contains=pythonExpression

@Griffin-Brome
Copy link

Thank you! This should be part of the standard Vim distribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment