【问题标题】:Snippets with indentation in Visual Studio CodeVisual Studio Code 中的缩进片段
【发布时间】:2017-06-27 18:03:06
【问题描述】:

我正在尝试在 Visual Studio Code 中创建一个 sn-p。这可行,但缺少缩进:

我的 sn-p:

"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"
}

你所看到的:

<!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>

我想要什么:

<!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>

【问题讨论】:

    标签: visual-studio-code vscode-snippets


    【解决方案1】:

    我认为更合适的方法是使用\t而不是空格来保持文档缩进。

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

    【讨论】:

    • Visual Studio Code 会将制表符转换为您喜欢的缩进。
    【解决方案2】:

    缩进需要在字符串内部,而不是外部(没有意义的地方),所以:

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

    代替:

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

    这按预期工作:

    "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"
    }
    

    【讨论】:

    • 不要在字符串中硬编码缩进,最好使用制表符“\t”的转义序列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2016-09-05
    • 2016-06-14
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    相关资源
    最近更新 更多