13

I'm trying to create a snippet in Visual Studio Code. This works, but the indentation is missing:

My snippet :

"HTML structure": {
    "prefix": "html",
    "body": [
        "<!DOCTYPE html>",
        "<html lang='fr'>",
        "<head>",
            "<meta charset='UTF-8'>",
            "<meta name='viewport' content='width=device-width, initial-scale=1.0'>",
            "<meta http-equiv='X-UA-Compatible' content='ie=edge'>",
            "<title>$1</title>",
        "</head>",
        "<body>",
            "$2",
        "</body>",
        "</html>"
    ],
    "description": "Base template for html file"
}

What you see :

<!DOCTYPE html>
<html lang='fr'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<title>test</title>
</head>
<body>
test
</body>
</html>

What I'd like :

<!DOCTYPE html>
<html lang='fr'>
<head>
  <meta charset='UTF-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  <meta http-equiv='X-UA-Compatible' content='ie=edge'>
  <title></title>
</head>
<body>
</body>
</html>
Gama11
  • 31,714
  • 9
  • 78
  • 100
Jeremy
  • 1,756
  • 3
  • 21
  • 45

3 Answers3

25

I think the more appropriate way is to use \t instead of space to maintain document indentation even.

"\t<meta charset='UTF-8'>",
  • 3
    Visual Studio Code will convert the tab character into your preferred indentation. – Seth Nov 15 '20 at 10:51
10

The indentation needs to be inside of the strings, not outside (where it's meaningless), so:

"  <meta charset='UTF-8'>",

instead of:

  "<meta charset='UTF-8'>",

This works as intended:

"HTML structure": {
    "prefix": "html",
    "body": [
        "<!DOCTYPE html>",
        "<html lang='fr'>",
        "<head>",
        "  <meta charset='UTF-8'>",
        "  <meta name='viewport' content='width=device-width, initial-scale=1.0'>",
        "  <meta http-equiv='X-UA-Compatible' content='ie=edge'>",
        "  <title>$1</title>",
        "</head>",
        "<body>",
        "  $2",
        "</body>",
        "</html>"
    ],
    "description": "Base template for html file"
}
Gama11
  • 31,714
  • 9
  • 78
  • 100
  • 1
    Instead of having indentation as hardcoded in strings, you should better use the escape sequence for tab character "\t". – Fahad Javed Aug 05 '19 at 18:10
0

I ran into this today and wanted to provide a more "robust" answer.

create a file in the .vscode folder called html.code-snippets please the contents in like i have done here:

{
"client-page": {
    "scope": "html",
    "prefix": "sn-client-page",
    "body": [
        "<ion-content>",
        "\t<ion-grid class=\"grid-full-height\">",
        "\t\t<ion-row>",
        "\t\t\t<ion-col>",
        "\t\t\t\t<header>",
        "\t\t\t\t\t<app-client-name></app-client-name>",
        "\t\t\t\t</header>",
        "\t\t\t</ion-col>",
        "\t\t\t<ion-col>",
        "\t\t\t\t<header>title</header>",
        "\t\t\t</ion-col>",
        "\t\t</ion-row>",
        "\t\t<ion-row>",
        "\t\t\t<ion-col>",
        "\t\t\t\t<!---content-->",
        "\t\t\t</ion-col>",
        "\t\t\t<ion-col>",
        "\t\t\t\t<!---content-->",
        "\t\t\t</ion-col>",
        "\t\t</ion-row>",
        "\t</ion-grid>",
        "</ion-content>",
    ]
  }
}

I can now start typing "sn" (without the quotes) in a vscode html file and will see the snippet name and hit enter... the output is this!

screenshot of html output

Please note the using \t each time represents amount of tabs/spaces/ indention. I hope this helps someone. Thanks.

Judson Terrell
  • 4,204
  • 2
  • 29
  • 45