kind of trivial question but here it goes. I can't figure out why VS Code autocompletes (with tab) all html tags correctly while php tag not. When i type "php" and hit tab, it creates this:

<php></php>

which is useless and i have no idea why it's there. I want it to do normal php tag

<?php ?>

I have turned off suggestions as they were distracting me. Anyway to edit default/top "tab autocomplete" item?


Solution 1:

this hack worked for me: go to 'Preferences: Open User Snippets', and paste this snippet into the html (yes, that's html) user snippets:

"php": {
    "prefix": "php",
    "body": [
        "<?php $1 ?>"
    ],
    "description": "php tag"
}

Solution 2:

I would elaborate on Lane's answer by putting the final cursor represented by $0 in a new line between the tags. In the snippet, each comma inside the "body" array represents a new line.

So in Visual Sutiio Code go to File > Preferences > User Snippets search for "html.json" and edit it by pasting this:

{
  // Place your snippets for html here. 
  "php": {
    "prefix": "php",
    "body": ["<?php", "$0", "?>"],
    "description": "php tag"
  }
}

Solution 3:

In Visual Studio Code, go to File > Preferences > User Snippets > html.json file and then paste the following code:

"php": {
    "prefix": "php",
    "body": [
        "<?php $1 ; ?>",
        "$2"
    ],
    "description": "php tag"
}

Solution 4:

Under File > Preferences there is User Snippets, use html for adding php snippets.

{
  "Snippet name": {
  "prefix": ["keywords", "for", "finding"],
  "body": [
    "<?php ${1:placeholder} ?>"
  ],
  "description": "A php tag."
  }
}

$1, $2 etc. are variables that will be on focus when snippet is entered. You can also use placeholder as presented in my example.

For multiline body, end current line with comma and add content at new line in " ". You can find more information about creating own snippets in VS Code here:

https://code.visualstudio.com/docs/editor/userdefinedsnippets

Solution 5:

go in File > Preferences > User Snippets now write in bar php and press enter .

vs code generate file called php.code-snippets , now here paste this :

  { 

    // Place your GLOBAL snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
    // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
    // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
    // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
    // Placeholders with the same ids are connected.
    // Example:
    "php": {    
        "prefix": "php",
        "body": [
            "<?php //put here code ?>"
        ],
        "description": "php tag"
    } 
}