Skip to content

Instantly share code, notes, and snippets.

@movalex
Forked from mdzhang/python.vim
Created April 10, 2019 10:37
Show Gist options
  • Save movalex/0510a591ff6daec16ba96f0961913a48 to your computer and use it in GitHub Desktop.
Save movalex/0510a591ff6daec16ba96f0961913a48 to your computer and use it in GitHub Desktop.
Vim - SQL highlighting in Python strings
" Put this in ~/.vim/after/syntax/python.vim
" Slight tweaks to https://lonelycoding.com/can-i-use-both-python-and-sql-syntax-highlighting-in-the-same-file-in-vim/
" Needed to make syntax/sql.vim do something
unlet b:current_syntax
" Load SQL syntax
syntax include @SQL syntax/sql.vim
" Need to add the keepend here
syn region pythonString matchgroup=pythonQuotes
\ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
\ contains=pythonEscape,@Spell keepend
syn region pythonRawString matchgroup=pythonQuotes
\ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
\ contains=@Spell keepend
syn region SQLEmbedded contains=@SQL containedin=pythonString,pythonRawString contained
\ start=+\v(ALTER|BEGIN|CALL|COMMENT|COMMIT|CONNECT|CREATE|DELETE|DROP|END|EXPLAIN|EXPORT|GRANT|IMPORT|INSERT|LOAD|LOCK|MERGE|REFRESH|RENAME|REPLACE|REVOKE|ROLLBACK|SELECT|SET|TRUNCATE|UNLOAD|UNSET|UPDATE|UPSERT)+
\ end=+;+
let b:current_syntax = "pysql"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment