Search code examples
phpvisual-studio-codevar-dump

var_dump shortcut access in Visual Studio Code


how can I get quick access to var_dump in Visual Studio Code editor? For example: I enter vd letters and after pressing the Enter key I get var_dump("");.


Solution

  • VS Code has a global snippets file, as well as snippet files for each language. You can choose to add the snippet in either one.

    Add it to the global file

    1) File-->Preferences-->User snippets

    2) Select New Global Snippets file

    3) Save it as global.code-snippets (or name it anything with the extention code-snippets)

    4) Remove everything then paste this in:

    {
      "Var_Dump": {
        "prefix": "vd",
        "scope": "php",
        "body": [
          "var_dump($1);",
          "$2"
        ],
        "description": "var_dump"
      }
    }
    

    Or Add it to the php file

    1) File-->Preferences-->User snippets-->php.json

    2) Remove everything then paste this in:

    {
      "Var_Dump": {
        "prefix": "vd",
        "body": [
          "var_dump($1);",
          "$2"
        ],
          "description": "var_dump"
        }
    }
    

    Usage

    Go to a php file (make sure it has opening <?php tag. Type "vd" and hit tab

    Not working?

    • VS Code says you don't need to restart your editor, however I found this helped.

    • Snippets/intellisense can take some time to come up. Wait for the snippet to show up before hitting tab. You are waiting for this: enter image description here

    More info and options can be found on this VS Code help page.