Search code examples
sublimetext3sublimetext

How to specify the scope of a snippet between a PHP or a HTML file?


It seems that when examining the scope of a PHP file, whose extension is .php, and whose syntax is correctly recognised as PHP, Sublime will still report it as text.html.basic (among others). This makes my HTML snippet always execute in it.

Since what I want is to make a snippet that has todo as its tabTrigger, to insert a commented out tag, obviously i need it to be different for the different filetypes (<!-- --> or /* */). But this way, the <!-- --> comment gets inserted into the PHP file...

Is there an extra detail i'm missing, or is this a bug?


Solution

  • The PHP syntax in Sublime Text is an extension of the HTML syntax, meaning all HTML is valid in a .php file, plus the extra <?php … ?> tag. If your cursor is in a .php file, but outside the <?php tag, the scope will be embedding.php text.html.basic and comments should take the <!-- … --> HTML form.

    Once you're inside that PHP tag, you now have an additional scope of source.php. Comments now should use PHP comments /* … */ and/or // ….

    For your snippet, just set the scope as source.php, and it should only trigger inside actual PHP code.

    If you want your snippet to trigger only in HTML, and not in PHP, set the source to text.html.basic -source.php.

    Note This is all true in the most recent version of Sublime Text 3 (3.2.2, Build 3211). The PHP syntax was entirely refactored earlier, so this may not work correctly with earlier builds. If you haven't upgraded, now is a great time!