2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCodeのユーザースニペットにPowerShellスクリプトのテンプレートを登録する

Posted at

普段、PowerShellスクリプトを作成するときに自分は下記のようなテンプレートを下敷きにして作成しています。

#!/usr/bin/env pwsh
<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.EXAMPLE
    PS C:\> <example usage>
    Explanation of what the example does
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
    General notes
#>

PROCESS {

    Set-StrictMode -Version Latest
    $ErrorActionPreference = "stop"

}

今回はこのテンプレートをVSCodeのユーザースニペットに登録して便利に入力できるように設定していきます。

今回利用する環境

Windows 11 23H2
VSCode 1.91.1

参考ドキュメント

Create your own snippets

ユーザースニペットを登録する

VSCodeでは標準機能でユーザー独自のスニペットを設定できるため、この機能を利用してPowerShellのテンプレートを登録し、スニペットから入力してみます。

スニペットのスコープ管理は、すべての言語に登録するか言語を指定して登録するか選択できるため、今回はPowerShellの場合のみ表示できるスニペットに登録します。

コマンドパレット(Ctrl+Shift+P)からユーザースニペットの構成を選択。

image.png

選択項目として言語が出てくるため、PowerShellを選択

image.png

powershell.jsonが生成されるため、こちらに利用したいスニペットを記載していきます。

image.png

作成したpowershell.jsonにスニペットを登録する

ドキュメントを参照すると、json形式でプレフィックス名とprefixとbodyがあれば動くようなので、今回はこれらの要素を登録します。

{
	// 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:
	// $1, $2 for tab stops, $0 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('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"ps1template":{
		"prefix":"ps1template",
		"body":[
			"#!/usr/bin/env pwsh",
			"<#",
			".SYNOPSIS",
			"    Short description",
			".DESCRIPTION",
			"    Long description",
			".EXAMPLE",
			"    PS C:\\> <example usage>",
			"    Explanation of what the example does",
			".INPUTS",
			"    Inputs (if any)",
			".OUTPUTS",
			"    Output (if any)",
			".NOTES",
			"    General notes",
			"#>",
			"",
			"PROCESS {",
			"",
			"    Set-StrictMode -Version Latest",
			"    \\$ErrorActionPreference = \"stop\"",
			"",
			"}",
			""
		]
	}
}

上記はps1templateという名前でスニペットからテンプレートを入力できるような登録になります。

一点、補足する事項としては$ErrorActionPreferenceの変数の部分は先頭に$含み、ユーザースニペットでは$をつかった機能があり、今回は単純に文字として$を挿入したいため、\\$と入力してエスケープしています。

スニペットから登録したテンプレートを入力してみる

image.png

言語モードがPowerShellの際にスニペット(ctrl+space)を確認すると、先ほど登録したps1templateが表示されます。

こちらを選択するとテンプレートが挿入されます。

image.png

総評

今回はテンプレートを登録するだけですが、ドキュメントをみると変数を展開したり日付を挿入したりと、色々とカスタマイズできるのでスニペットで利用できる機能を一度見ておくと、便利に使える要素が見当たるかもしれません。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?