I have C-style comments inside PHP strings that contain SQL code, so I can easily understand what the query is doing when I see it running on the server.

DB::runQuery('/* this is my SUPER complicated update example */

    UPDATE table1 SET foo = "bar"

How could I add a rule to style that comment-in-a-string differently to the rest of the string?

Thank you!

1 Answer 1


If the syntax file already matches the /**/ comment in the string, you only need to add a highlight for it.

  1. Check the syntax group (e.g., using zS from scriptease or synstax#UnderCursor() from synstax).

  2. Add highlighting in ~/.vim/after/syntax/php.vim (or your Vim directory equivalent):

    highlight [opts] GROUP [stuff] " see :help :highlight

If the syntax file doesn't already match the comment in the string… you could try to hack one in (place a :syntax command in ~/.vim/after/syntax/php.vim or equivalent). You could also try a solution using :help textprop or :help matchadd().

  • I managed to get it working with autocmd ColorScheme * highlight CommentInString guifg=#fcffc7 guibg=#282c34 autocmd ColorScheme * match CommentInString /'\/*.\+*\//
    – Codemonkey
    Commented Sep 19, 2023 at 14:43
  • Any idea how I could highlight the words inside the comment different to the /* and ` */`... I tried messing around with regions and couldn't get anything to work. I'd quite like to have the words underlined, but not the comment tags, or any leading/trailing whitespace
    – Codemonkey
    Commented Sep 19, 2023 at 14:55
  • @Codemonkey I’m not really an expert in :syntax. Maybe a new question?
    – D. Ben Knoble
    Commented Sep 20, 2023 at 13:10

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.