Skip to content

Instantly share code, notes, and snippets.

@versusvoid
Created September 13, 2026 07:54
Show Gist options
  • Select an option

  • Save versusvoid/ccc50e9a8b7f600019d3f357778708f3 to your computer and use it in GitHub Desktop.

Select an option

Save versusvoid/ccc50e9a8b7f600019d3f357778708f3 to your computer and use it in GitHub Desktop.
Vim SQL syntax highlighting in alembic migrations
" place it into ~/.config/vim/after/syntax/python.vim
" or ~/.config/nvim/after/syntax/python.vim for neovim
unlet! b:current_syntax
syntax include @SQL syntax/sql.vim
" starts with `op.execute("` and not followed by "
" (\ze — lookahead — checking that next char is not ", but not including it into pattern)
" `hs=e` — highlight last char of match as python quote
" `rs=e` — sql region starts afterward
"
" ends with `")` not preceeded by "
" (\ze — lookbehind — symmetric check for preceeding ")
" `he=s` — highlight first char of match as python quote
" `rs=e` — sql region ends before
syntax region sqlInjection
\ matchgroup=pythonQuotes
\ start=+\<op.execute("\ze[^"]+hs=e,rs=e
\ end=+[^"]\zs")+he=s,re=s
\ contains=@SQL
\ keepend
" same for ' quotes
syntax region sqlInjection
\ matchgroup=pythonQuotes
\ start=+\<op.execute('\ze[^']+hs=e,rs=e
\ end=+[^']\zs')+he=s,re=s
\ contains=@SQL
\ keepend
" variation for multiline strings, arbitrary space (including newlines) between `(`, `'''` and `)`
" no lookahead/lookbehind, `hs` offset by 2 to highlight triplequotes
syntax region sqlInjection
\ matchgroup=pythonQuotes
\ start=+\<op.execute(\(\s\|\n\)*"""+hs=e-2,rs=e
\ end=+"""\(\s\|\n\)*)+he=s+2,re=s
\ contains=@SQL
\ keepend
syntax region sqlInjection
\ matchgroup=pythonQuotes
\ start=+\<op.execute(\(\s\|\n\)*'''+hs=e-2,rs=e
\ end=+'''\(\s\|\n\)*)+he=s+2,re=s
\ contains=@SQL
\ keepend
let b:current_syntax = "python"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment