Created
September 13, 2026 07:54
-
-
Save versusvoid/ccc50e9a8b7f600019d3f357778708f3 to your computer and use it in GitHub Desktop.
Vim SQL syntax highlighting in alembic migrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " 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