I have the following regex to stylize SQL keywords within a string (keywords shortened for readability):

autocmd Syntax * syn match pythonSql     /\v("|')\s*([A-Z]{3,}<.*)@=\zs(SELECT|FROM|WHERE)
                                          \ze(\s|\()\_.*\1/ containedin=.*String

This seems to work well for the first term, but is then skipping all the rest of the terms. That is, it is doing:

  • "SELECT * FROM table WHERE id=1", that is, marking up SELECT, but not FROM or WHERE

What I'm trying to have the regex do is (a bit of a crude way, but good enough for my purposes:

  • For text that starts with " or '...
  • And, ignoring spaces, the first word after is all-caps of length 3 or more followed by a space (such as SELECT, INSERT, UPDATE, etc..)
  • Highlight all of the keywords up until the terminating string (ignoring string escapes).

How could this be improved to capture all the SQL keywords and not just the first one?

1 Answer 1


For this particular case, use syntax keyword instead. If you use it with both contained and containedin=pythonString, that will restrict it to highlighting these keywords only within a Python string.

:syn keyword pythonSql SELECT FROM WHERE contained containedin=pythonString

Instead of using an explicit autocmd, I'd recommend executing this command from ~/.vim/after/syntax/python.vim, which will be automatically loaded after the main Python syntax file gets loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.