Last active
December 8, 2021 05:39
-
-
Save mdzhang/eaab47b323d49feb5db81a3b92fc128c to your computer and use it in GitHub Desktop.
Vim - SQL highlighting in Python strings
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
" 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
This is great! One thing I modified was adding
--
(start=+\v(--|ALTER|BEGIN|...
) to catch strings that start with a comment.The other thing I can't seem to figure out how to solve is it doesn't work for me with arrays containing triple quotes. For example: