16

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>

3 Answers 3

29

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

"\t<meta charset='UTF-8'>",
Sign up to request clarification or add additional context in comments.

1 Comment

Visual Studio Code will convert the tab character into your preferred indentation.
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"
}

1 Comment

Instead of having indentation as hardcoded in strings, you should better use the escape sequence for tab character "\t".
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.