Search code examples
phpsqlvimvim-syntax-highlighting

Real SQL syntax highlighting in PHP scripts with Vim


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"?


Solution

  • 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;