在 VS 代码片段中为 Powershell 变量保留 $

Keep $ for Powershell variable in VS code snippet

我可以在 Powershell 的 VS 代码中使用制表符间距保存我的代码片段,但是当我调用代码片段时,它不会显示变量的 $,从而一直忽略我的变量。它只会粘贴名称并省略 $

当您 select 您的代码片段时,如何让 VS 代码粘贴到 $ 中?

这是我用于 "template" 代码片段的 VS 代码 JSON 文件

{
    // Place your snippets for powershell here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // ,  for tab stops, [=10=] for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('');",
    //      ""
    //  ],
    //  "description": "Log output to console"
    // }

"Template": {
    "prefix": "template",
    "body": [
        "<#",
        ".SYNOPSIS",
        "\t<Overview of script>",
        "",
        ".SYNTAX",
        "\t<Cmdlet-Name> -Parameter <value>",
        "",
        ".DESCRIPTION",
        "\t<Brief description of script>",
        "",
        ".PARAMETER <Parameter_Name>",
        "\t<Brief description of parameter input required. Repeat this attribute if required>",
        "",
        ".INPUTS",
        "\t<Inputs if any, otherwise state None>",
        "",
        ".OUTPUTS",
        "\t<Outputs if any, otherwise state None - example: Log file stored in C:\file.log>",
        "", 
        ".EXAMPLE",
        "\t<Example goes here. Repeat this attribute for more than one example>",
        "",
        ".REMARKS",
        "\tVersion:        1.0",
        "#>",
        "",
        "#---------------------------------------------------------[Variables]------------------------------------------------------------",
        "",
        "$var1 = <stuff>",
        "$var2 = <stuff>",
        "",
        "#---------------------------------------------------------[Import Modules]--------------------------------------------------------",
        "",
        "Import-Module <name>",
        "",
        "#-----------------------------------------------------------[Functions]------------------------------------------------------------",
        "",
        "Function <FunctionName> {",
        "\t[CmdletBindinging()]",
        "\tparam(",
        "\t\t[Parameter()]",
        "\t\t[string]$MyOptionalParameter,",
        "",
        "\t\t[Parameter(Mandatory)]",
        "\t\t[int]$MyMandatoryParameter",
        "\t)",
        "",   
        "\tTry{",
        "\t\t<code goes here>",
        "\t}",
        "", 
        "\tCatch {",
        "\t\tWrite-Host $Error.Exception",
        "\t\t$Error.Exception | Out-File Out-File $env:TEMP\file.log",
        "\t\tBreak",
        "\t}",
        "",
        "\tFinally {",
        "\t\t$time = Get-Date",
        "\t\tcompleted at $time | Out-File $env:TEMP\file.log",
        "\t}",
        "}",
        "",
        "#-----------------------------------------------------------[Execution]------------------------------------------------------------",
        "",
        "<FunctionName>",
    ],
    "description": "test"
}

}

当我调用此代码段将模板粘贴到新的 .ps1 文件时,它省略了所有 $。你如何让他们留下来?

在 Visual Studio 代码片段正文中使用 \$ 以嵌入 文字 $ .

例如,要嵌入 PowerShell 变量 $HOME,请使用 \$HOME.

注意:从片段解析器的角度来看,转义需要一个 single \,但由于片段被定义为 JavaScript strings,它们本身使用 \ 作为转义字符,需要 \ 才能将单个 \ 传递给代码段解析器。

参见 the docs


顺便说一句:

使用$$ 不小心,有点的作用,但它的目的不是逃避,它会导致不同的行为 :

$$ 前缀标识符的位置变为 制表位,因为 Visual Studio 代码解释序列如下:

第一个 $ 变成文字 [1],但第二个 $ 和后面的标识符被解释为 Visual Studio代码变量引用;如果不存在该名称的内置变量,则将其用作 placeholder 文本。


[1] 任何 $ not 后跟有效的 Visual Studio 代码占位符名称或变量引用按字面意思处理。