9

I know it's possible to enable SQL syntax highlighting in PHP scripts using the option

let php_sql_query=1

But this just enables highlighting of all SQL keywords in every string. Even in a normal sentence like this one.

Is there a way to only enable this for strings starting with "Select", "update" or "delete"?

4
  • 1
    Given that there is so much LAMP (MySQL and PHP) programming, and so many hard core coders using vim, you would think there would be dozens of plugins for this, or at least an attempt to answer this question. Commented Apr 25, 2012 at 19:06
  • check out vim.org/scripts/script.php?script_id=492 Commented Apr 29, 2012 at 3:38
  • @sdjuan Formatting is nice, thanks for the plugin, but this does not fix the broken syntax highlighting. Commented May 10, 2012 at 8:13
  • Any idea if there's better solutions for this, 8 years down the line? As per my comment on the accepted answer, it really seems like a very poor-quality solution. Commented May 14, 2020 at 16:01

1 Answer 1

13

Enclosing the query in a heredoc with an identifier of "SQL" triggers Vim to do SQL syntax highlighting in the block, e.g.:

$q = <<<SQL
        SELECT `foo`
        FROM `db`.`table`
        WHERE `foo` = 'bar'
SQL;
Sign up to request clarification or add additional context in comments.

2 Comments

I mean, just barely. It'll highlight the clauses (SELECT/WHERE/GROUP/etc) and AND/OR, and numbers, but as far as I can make out everything else (table names, column names, mysql functions, strings) all end up sytactically as phpRegion->phpHereDoc. I know your answer is from 8 years ago; any idea if there's a more complete solution these days?
Actually I take that back, if you qualify every table name and column name with backticks you WILL get syntax highlighting. If not then you're out of luck.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.