Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snippets with indentation in Visual Studio Code

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>
like image 739
Jeremy Avatar asked Jun 27 '17 18:06

Jeremy


2 Answers

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

"\t<meta charset='UTF-8'>",
like image 116
IdontCareAboutReputationPoints Avatar answered Oct 08 '22 19:10

IdontCareAboutReputationPoints


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"
}
like image 31
Gama11 Avatar answered Oct 08 '22 21:10

Gama11